Skip to content
cppbeginner

C++ Basics

Variables, loops and functions

6 questions

By EZ4Code Team

1. What is the standard output stream object in C++?

std::cout
std::out
printf
System.out
Explanation: std::cout is the standard output stream object, used with the << operator; requires #include <iostream>.

2. What does the following code output? int x = 5; std::cout << (x / 2);

int x = 5;
std::cout << (x / 2);
2
2.5
3
Error
Explanation: Integer division truncates the decimal part; 5/2 results in an int value of 2; to get 2.5 you need 5.0/2 or (double)x/2.

3. What is the main difference between a reference and a pointer?

A reference must be initialized and cannot be rebound; a pointer can be null and repointed
A reference can be null
A pointer must be initialized
They are exactly the same
Explanation: A reference is an alias; it must be initialized at definition and cannot be rebound to another object, and cannot be null; a pointer can be null and can be repointed.

4. What is the difference between const int* p and int* const p?

The former cannot modify the pointed-to data through p; the latter cannot modify p itself (the pointer)
They are exactly the same
The former cannot modify p's pointing
The latter cannot modify the data
Explanation: const on the left of * modifies the data (pointer to constant); const on the right of * modifies the pointer itself (constant pointer).

5. How many times does the loop body of for(int i=0;i<3;i++) execute?

3 times (i=0,1,2)
4 times
2 times
0 times
Explanation: i starts at 0; each iteration checks i<3, executes the body if true, then i++; the body executes 3 times (i=0,1,2).

6. What is function overloading based on?

Different number or types of parameters (return type is not considered)
Different return type
Different function names
Different scope
Explanation: C++ function overloading requires functions with the same name to have different parameter lists (number/type/order); return type does not participate in overload resolution.

More cpp Quizzes