Alright, let's get you prepped for your C++ programming fundamentals lab viva!
Here's a
breakdown of common question types and specific examples, covering the core concepts:
I. Basic Syntax & Concepts
● Q: What is C++?
○ A: C++ is a general-purpose programming language. It's an extension of the C
language, with added features like object-oriented programming (OOP).
● Q: What is the difference between C and C++?
○ A: C is a procedural language, while C++ supports both procedural and
object-oriented programming. C++ has features like classes, objects, inheritance,
polymorphism, and operator overloading, which are not present in C.
● Q: Explain the structure of a basic C++ program.
○ A: Typically, it includes:
■ Header files (e.g., #include <iostream>)
■ using namespace std; (for standard library access)
■ The main() function (the program's entry point)
■ Statements within the main() function.
● Q: What are header files? Give examples.
○ A: Header files contain declarations of functions and variables. Examples:
<iostream> (for input/output), <string> (for string manipulation), <cmath> (for
mathematical functions).
● Q: What is namespace std;?
○ A: It makes the standard C++ library's components (like cout, cin) accessible
without needing to prefix them with std::.
● Q: What is the main() function?
○ A: It's the starting point of execution for any C++ program.
● Q: What are variables? What are data types?
○ A: Variables store data. Data types define the kind of data a variable can hold (e.g.,
int, float, char, bool).
● Q: Explain the different data types in C++.
○ A: int (integers), float (floating-point numbers), double (double-precision
floating-point), char (characters), bool (boolean values), string (sequences of
characters).
● Q: What are operators in C++? Give examples.
○ A: Operators perform operations on operands. Examples: arithmetic (+, -, *, /),
relational (==, !=, >, <), logical (&&, ||, !).
● Q: What is the difference between = and ==?
○ A: = is the assignment operator (assigns a value to a variable). == is the equality
operator (checks if two values are equal).
● Q: What are comments in C++? How do you write them?
○ A: Comments are used to explain code. Single-line comments start with //, and
multi-line comments are enclosed in /* ... */.
II. Control Flow
● Q: Explain if, else if, and else statements.
○ A: They allow conditional execution of code blocks based on whether conditions are
true or false.
● Q: Explain the switch statement.
○ A: It provides a multi-way branching mechanism, allowing you to select one of many
code blocks to execute based on the value of an expression.
● Q: What are loops? Explain for, while, and do-while loops.
○ A: Loops repeat a block of code multiple times.
■ for: Used when you know the number of iterations.
■ while: Continues as long as a condition is true.
■ do-while: Executes at least once, then continues as long as a condition is
true.
● Q: What is the difference between while and do-while loops?
○ A: while checks the condition before executing the loop body. do-while checks the
condition after executing the loop body.
● Q: What are break and continue statements?
○ A: break exits a loop or switch statement. continue skips the rest of the current loop
iteration and proceeds to the next iteration.
III. Functions
● Q: What is a function?
○ A: A block of code that performs a specific task.
● Q: What are the advantages of using functions?
○ A: Code reusability, modularity, improved readability.
● Q: Explain function prototypes, function definitions, and function calls.
○ A: A prototype declares the function. A definition contains the function's code. A call
executes the function.
● Q: What are parameters and arguments?
○ A: Parameters are variables declared in a function's definition. Arguments are the
actual values passed to the function when it's called.
● Q: What is pass by value and pass by reference?
○ A: Pass by value creates a copy of the argument, so changes inside the function
don't affect the original. Pass by reference passes the memory address of the
argument, so changes inside the function do affect the original.
● Q: What is a return value?
○ A: The value a function sends back to the calling code.
● Q: What is recursion?
○ A: A function calling itself.
IV. Arrays & Strings
● Q: What is an array?
○ A: A collection of elements of the same data type stored in contiguous memory
locations.
● Q: How do you declare and initialize an array?
○ A: int myArray[5]; (declaration), int myArray[5] = {1, 2, 3, 4, 5}; (initialization).
● Q: How do you access elements of an array?
○ A: Using their index (starting from 0). Example: myArray[0].
● Q: What is a string in C++?
○ A: A sequence of characters.
● Q: How do you declare and initialize a string?
○ A: string myString = "Hello";
● Q: What are some common string functions?
○ A: length(), append(), find(), substr().
V. Input/Output
● Q: Explain cin and cout.
○ A: cin is used for input from the standard input stream (keyboard). cout is used for
output to the standard output stream (console).
● Q: What is the << and >> operator?
○ A: << is the insertion operator (used with cout). >> is the extraction operator (used
with cin).
Tips for Your Viva:
● Practice: Write and run code examples for each concept.
● Explain Clearly: Don't just memorize definitions; understand and explain them.
● Be Prepared for Code Tracing: Your examiner might give you a small code snippet and
ask you to predict its output.
● Be Confident: Even if you don't know an answer, try to explain your thought process.
Good luck with your lab viva!