What does the following code output?
int wage = 10;
cout << wage * 2;
What is short-circuit evaluation?
When logical expressions stop evaluating as soon as the result is known.
What keyword defines a class in C++?
class
What keyword starts a function declaration?
Return type (e.g., int, void)
Which header is needed for file input/output?
<fstream>
What is the keyword used to declare a variable of type integer in C++?
int
What is wrong with this code?
if (x = 5) {
cout << "Yes";
}
It uses assignment (=) instead of comparison (==)
What is an object?
An instance of a class.
What is the return type of this function?
int add(int a, int b) {
return a + b;
}
int
What does this line do?
ifstream inFile("data.txt");
Opens the file data.txt for reading.
What header file is required to use cin and cout
<iostream>
Describe the difference between while and do-while loops.
do-while always runs at least once; while may not run at all.
What does the public keyword mean in a class?
Members can be accessed from outside the class.
What is the purpose of parameters in functions?
They allow passing data into a function.
What is a pointer?
A variable that stores a memory address.
Explain the purpose of endl in output statements.
It inserts a newline character and flushes the output buffer.
What is a potential risk of this loop?
while (true) {
// some condition never changes
}
Infinite loop risk
What is a constructor?
A special function that initializes objects of a class.
What is the * operator used for in pointers?
Dereferencing — accessing the value at a memory address.
What is the result of the following code and why?
int wage;
cout << wage;
It outputs an undefined value because wage was declared but not initialized.
Write a for loop that counts down from 5 to 1.
for (int i = 5; i >= 1; i--) {
cout << i;
}
Write a simple class Car with one public int member speed and no methods.
class Car {
public:
int speed;
};
What is the difference between function overloading vs overriding?
Overloading is defining multiple functions with the same name but different parameter lists.
Overriding lets you define one or more tasks that will override the function defined in the base class or parent class. WHO can do this by calling virtual prototypes or inheriting the type declared in your base class.
What will this code output?
int x = 10;
int* ptr = &x;
cout << *ptr;
10