1.
Which of the following is not a valid identifier?
Correct Answer
A. Return
Explanation
The identifier "return" is not valid because it is a reserved keyword in many programming languages, including Java. Reserved keywords have special meanings in the language and cannot be used as identifiers for variables, functions, or classes.
2.
What is the value of x after the following statements?
int x;
x = 0;
x = x + 30;
Correct Answer
B. 30
Explanation
The value of x is initially set to 0. Then, x is incremented by 30 using the statement "x = x + 30". Therefore, the final value of x is 30.
3.
What is the output of the following code?
float value;
value = 33.5;
cout << "value" << endl;
Correct Answer
C. Value
Explanation
The code initializes a variable "value" of type float and assigns it the value 33.5. The next line prints the string "value" followed by a newline character. Therefore, the output of the code is the string "value".
4.
Which of the following lines correctly reads a value from the keyboard and stores
it in the variable named myFloat?
Correct Answer
A. Cin >> myFloat;
Explanation
The correct answer is "cin >> myFloat;". This line of code uses the ">>" operator to read a value from the keyboard and store it in the variable named myFloat. The "cin" object is used to read input from the keyboard, and the ">>" operator is used to extract the input and store it in the variable.
5.
Another way to write the value 3452211903 is
Correct Answer
A. 3.452211903e09
Explanation
The given answer, 3.452211903e09, represents the scientific notation of the value 3452211903. In scientific notation, a number is expressed as a decimal number between 1 and 10, multiplied by a power of 10. In this case, the decimal number is 3.452211903 and it is multiplied by 10 raised to the power of 9. This notation is commonly used to represent very large or very small numbers in a concise and standardized format.
6.
What is the value of x after the following statements?
float x;
x = 15/4;
Correct Answer
C. 3.0
Explanation
The value of x after the given statements is 3.0. In the statement x = 15/4, the division operation is performed between two integers, which results in an integer division. In integer division, the decimal part is truncated, and only the whole number part is considered. Therefore, 15 divided by 4 equals 3 with a remainder of 3. The remainder is discarded, and the value assigned to x is the whole number part, which is 3.0 in this case.
7.
What is the value of x after the following statements?
int x;
x = 15 %4;
Correct Answer
C. 3
Explanation
The value of x after the given statements is 3. The expression "15 % 4" calculates the remainder when 15 is divided by 4, which is 3. Therefore, the value of x is assigned as 3.
8.
What is the value of x after the following statement?
float x;
x = 3.0 / 4.0 + 3 + 2 / 5
Correct Answer
D. 3.75
Explanation
The value of x is 3.75. In the given statement, the expression is evaluated from left to right. The first operation is 3.0 divided by 4.0, which equals 0.75. Then, 0.75 is added to 3, resulting in 3.75. Finally, 2 divided by 5 is 0.4, but since it is an integer division, the result is 0. Therefore, the final value of x is 3.75.
9.
What is the value of x after the following statements?
double x;
x = 0;
x += 3.0 * 4.0;
x -= 2.0;
Correct Answer
C. 10.0
Explanation
The variable x is declared as a double and initialized with the value 0. Then, the value of x is updated by adding the result of multiplying 3.0 and 4.0, which is 12.0. Finally, the value of x is updated again by subtracting 2.0. Therefore, the final value of x is 10.0.
10.
If x has the value of 3, y has the value of -2, and w is 10, is the following
condition true or false?
if( x < 2 && w < y)
Correct Answer
B. False
Explanation
The condition in the if statement is false because both parts of the logical operator && (AND) need to be true for the overall condition to be true. In this case, x is not less than 2, so the first part of the condition is false. Therefore, the overall condition is false.
11.
What is the correct way to write the condition y < x < z?
Correct Answer
D. ((y < x) && (x < z))
Explanation
The correct way to write the condition y < x < z is ((y < x) && (x < z)). This is because the condition requires both y to be less than x and x to be less than z in order for it to be true. Using the logical AND operator (&&) ensures that both conditions must be satisfied for the overall condition to be true.
12.
Given the following code fragment, what is the output?
int x=5;
if( x > 5)
cout << "x is bigger than 5. ";
cout <<"That is all. ";
cout << "Goodbye\n";
Correct Answer
C. That is all. Goodbye
Explanation
The output of the code will be "That is all. Goodbye". This is because the condition in the if statement is false (x is not greater than 5), so the code inside the if statement will not be executed. The code after the if statement will be executed, which includes printing "That is all. " and "Goodbye".
13.
Executing one or more statements one or more times is known as:
Correct Answer
B. Iteration
Explanation
Iteration refers to the process of executing one or more statements repeatedly. It involves the use of loops to repeatedly perform a set of instructions until a certain condition is met. This allows for efficient and controlled repetition of code, making it easier to handle tasks that require repeated execution. Therefore, iteration is the correct term for executing statements multiple times.
14.
Given the following code fragment, what is the final value of y?
int x, y;
x = -1;
y = 0;
while(x <= 3)
{
y += 2;
x += 1;
}
Correct Answer
B. 10
Explanation
The code fragment initializes x to -1 and y to 0. Then, it enters a while loop that continues as long as x is less than or equal to 3. Inside the loop, y is incremented by 2 and x is incremented by 1. This process continues until x becomes 4, at which point the loop exits. Therefore, the final value of y is 10.