1.
What is the output of the following program?
int x = 10
int main(){
int y = x;
cout << y << endl
Correct Answer
B. 10
10
Explanation
The program declares a variable x and assigns it the value 10. In the main function, a variable y is declared and assigned the value of x. Then, the value of y is printed to the console. Since y is assigned the value of x, which is 10, the output will be 10.
2.
Int main(){
int x = 10;
int y = 5;
________{
cout << "x is greater than y";
return 0;
}
Correct Answer
if(x > y),if( x > y),if(x>y)
Explanation
The correct answer is "if(x > y),if( x > y),if(x>y)". This is because the code is checking if the value of x is greater than the value of y, and if it is, it will print "x is greater than y". The if statement is used to conditionally execute a block of code if the specified condition is true. In this case, there are three if statements, each checking the same condition, so the code will print the message three times if x is indeed greater than y.
3.
What is the output of the following program?
int x = 10;
int main(){
int x = 20;
cout << x;
return 0;
}
Correct Answer
C. 20
Explanation
The output of the program is 20 because the variable x is redefined inside the main function with a value of 20. When the program executes the line "cout
4.
Which of the following is not a correct variable type?
Correct Answer
B. Real
Explanation
The variable type "real" is not a correct variable type. In most programming languages, "real" is not recognized as a valid variable type. The correct variable types in this list are "float", "double", and "string". "Float" and "double" are used to represent decimal numbers, while "string" is used to represent a sequence of characters.
5.
Which of the following could be a comment? (check all the apply)
Correct Answer(s)
B. // comment
D. /*comment*/
Explanation
The correct answer is "// comment" and "/*comment*/" because both of these options are written in the correct format for a comment in programming languages. The "//" indicates a single-line comment, while "/* */" indicates a multi-line comment. The other options, "{comment}" and "/*comment/*", do not follow the correct syntax for comments and are therefore not valid comments.
6.
Which of the following is the correct operator to compare two variables?
Correct Answer
C. ==
Explanation
The correct operator to compare two variables is ==. This operator checks if the values of the two variables are equal. The = operator is used for assignment, not comparison. The := operator is not a valid comparison operator in most programming languages. The word "equals" is not a valid operator in programming, it is used in some programming languages as a method or function to compare values.
7.
What is the output of the following program?
int main(){
cout >> 10 << 11 <<endl;
Correct Answer
D. Compile Error
Explanation
The given program will result in a compile error. This is because the "
8.
What is the output of the following question if the input is 0?
int main(){
int x;
cin >> x;
if(!x){
cout << "hello";
}
else{
cout << "world";
}
return 0;
}
Correct Answer
C. Hello
Explanation
The code snippet asks for user input and stores it in the variable "x". If the value of "x" is 0, the condition "!x" will evaluate to true and the program will output "hello". Therefore, the output of the program when the input is 0 is "hello".
9.
What is the output of the following program?
int main(){
int x = 10;
int y = x++;
int z = ++y + (x/2);
cout << x << endl;
cout << y << endl;
cout << z << endl;
return 0;
}
Correct Answer
D. 11
11
16
Explanation
The program starts by initializing the variable x to 10. Then, the variable y is assigned the value of x, which is 10, and then x is incremented by 1 using the post-increment operator. So, after this step, x becomes 11 and y becomes 10.
Next, the variable z is assigned the value of y, which is 10, incremented by 1 using the pre-increment operator, which makes y equal to 11. Then, z is added to the result of x divided by 2, which is 5. So, z becomes 11 + 5 = 16.
Finally, the program prints the values of x, y, and z, which are 11, 11, and 16 respectively.
10.
What is the output of the following program?
int main()
{
bool x = -1;
x++;
if (x)
{
x = x % 3;
cout<<(x > ++x)<<endl;
}
else
cout<<x++<<endl; x = x++;
cout<<(x < x++)<<endl;
return 0;
}
Correct Answer
D. 0
0
Explanation
The program initializes a boolean variable x with the value -1, which is equivalent to true in C++. Then, it increments x by 1 using the post-increment operator. Since the value of x is now 0, the program enters the else block and prints the value of x (0) followed by a line break. After that, x is incremented again using the post-increment operator, but the value of x is still 0. Therefore, the expression (x < x++) evaluates to false. The program prints false followed by a line break. Hence, the output of the program is 0 followed by a line break, and then false followed by a line break.