Introduction:
C++ is a versatile and powerful programming language that offers a wide range of features for building various types of applications. As a programmer diving into the world of C++, understanding keywords, variable declarations, and operators is essential. In this blog post, we'll explore a quiz that covers some fundamental concepts in C++, along with explanations for each question.
Q1 of 10: Which one is not a C++ keyword?
- for
- class
- void
- Student
Q2 of 10: What is the output for the following
const int x;
x = 10;
cout << x;
return 0;
}
- Garbage Value
- 10
- Compilation Error
- Runtime Error
Q3 of 10: What output for the following code snippet:
{
int var = 10;
}
{
cout<< var;
}
return 0;
}
- 10
- 0
- Compilation Error
- Runtime Error
Q4 of 10: Variables declared inside a function or a block which is called?
- constant variables
- global variables
- static variables
- local variables
Q5 of 10: Which operator is used to signify the namespace?
- scope resolution operator [::]
- bitwise operator [&, |, !]
- ternary operator [?:]
- conditional operator
Q6 of 10: What will be the output of the following C++ code?
int a = 20;
int b = 10;
int c = 15;
int d = 5;
int e;
e = a + b * c / d;
cout << e << endl ;
return 0;
}
- 50
- 60
- 70
- 80
Q7 of 10: What is the output for the code?
int a = 9;
cout << (a << 2);
return 0;
}
- 2
- 36
- 1
- 10
Q8 of 10: What will be the output of the code if I provide the input as "C++ Programming"?
string name;
cin >> name;
cout << name;
return 0;
}
- C++ Programming
- Compilation Error
- Runtime Error
- C++
Q9 of 10: What will be the output of the following code snippet:
char a = 'a';
int b = a;
cout << b;
return 0;
}
- 'a'
- error
- 97
- 65
Q10 of 10: In a C++ program snippet, followings are used for definition of Integer variables?
signed s;
unsigned u;
long l;
long long ll;
- All of the above variable definitions are incorrect because basic data type int is missing
- All of the above variable definitions are correct because int is implicitly assumed in all of these
- Only “long l;” and “long long ll;” are valid definitions of variables
- Only “unsigned u;” is valid definition of variable" provide answer with solution
Answers with explanations:
Q1: Which one is not a C++ keyword?
Answer: Student
Explanation: "Student" is not a C++ keyword. The other options (for, class, void) are all valid C++ keywords.
Q2: What is the output for the given code?
Answer: Compilation Error
Explanation: The code attempts to modify a constant variable x, which is not allowed. The variable x is declared as a constant using const int x;, and then an attempt is made to assign a value to it using x = 10;. This will result in a compilation error.
Q3: What output for the given code snippet?
Answer: Compilation Error
Explanation: The variable var is declared within the first inner block and is not accessible in the second inner block. Therefore, trying to output var in the second inner block using cout << var; will result in a compilation error.
Q4: Variables declared inside a function or a block which is called?
Answer: Local variables
Explanation: Variables declared inside a function or a block are called local variables. They have a limited scope within that function or block.
Q5: Which operator is used to signify the namespace?
Answer: Scope resolution operator [::]
Explanation: The scope resolution operator :: is used to specify the namespace of a class or variable in C++.
Q6: What will be the output of the given code?
Answer: 50
Explanation: The code performs arithmetic operations on variables a, b, c, and d and then assigns the result to e. The value of e becomes 50, which is then printed using cout.
Q7: What is the output for the given code?
Answer: 36
Explanation: The code performs a left shift operation (a << 2) on the integer a, which results in 36. This value is printed using cout.
In the code, (a << 2) performs a left shift operation on the integer a by 2 positions.
Binary representation of 9 is 1001. When you left shift it by 2 positions, you get 100100, which is the binary representation of 36 in decimal.
So, (a << 2) results in 36, and that's what will be printed by cout.
Q8: What will be the output of the code with the provided input "C++ Programming"?
Answer: C++
Explanation: The cin statement only reads until whitespace, so it will read "C++" into the name variable. The subsequent cout statement outputs "C++".
Q9: What will be the output of the given code snippet?
Answer: 97
Explanation: The ASCII value of the character 'a' is 97, and it's assigned to the integer variable b. When b is printed using cout, it will display 97.
Q10: In a C++ program snippet, which variable definitions are used for Integer variables?
Answer: Only "long l;" and "long long ll;" are valid definitions of variables.
Explanation: The correct syntax for defining integer variables is to use only "long l;" and "long long ll;". The other options (signed s; and unsigned u;) are not complete and need an explicit data type like int before them to be valid.