Which if statement below tests if letter holds R? (letter is a char variable)
if ( letter == \"R\")
if ( letter >= \'R\')
if ( letter == R)
if ( letter = \'R\')
if ( letter == \'R\')
What are if statements used for in programs?
Repeating commands
Storing data
Numeric calculations
Numeric casts
Making decisions
The following if statement tests the rainfall in New York’s Central Park during the months of
June, July and August.
if (low <= rain && rain <= high)
System.out.println(\"Rainfall amount is normal.\");
else
System.out.println(\"Rainfall amount is abnormal.\");
It could be replaced with:
I.
if (rain >= low)
{
if (rain <= high)
System.out.println(\"Rainfall amount is normal.\");
}
else
System.out.println(\"Rainfall amount is abnormal.\");
II.
if (rain >= low)
{
if (rain <= high)
System.out.println(\"Rainfall amount is normal.\");
else
System.out.println(\"Rainfall amount is abnormal.\");
}
else
System.out.println(\"Rainfall amount is abnormal.\");
III.
if (rain >= low)
System.out.println(\"Rainfall amount is normal.\");
else if (rain <= high)
System.out.println(\"Rainfall amount is normal.\");
else
System.out.println(\"Rainfall amount is abnormal.\");
I only
II only
III only
II or III
I, II or III
What is output by the following code?
int x = 36 % 8;
if (x >= 10)
System.out.println( 1);
else if (x >= 8)
System.out.println( 2);
else if (x >= 6)
System.out.println( 3);
else if ( x >= 4)
System.out.println( 4);
else
System.out.println( 5);
1
2
3
4
5
Consider the code:
if ( y == 0 || x * y > 10)
Which of the following is an example of short circuit evaluation?
if x * y > 10 is false it evaluates y ==0
if x * y > 10 is false it doesn\'t evaluate y ==0
if y == 0 is false it doesn\'t evaluate x * y > 10
if y == 0 is true it doesn\'t evaluate x * y > 10
if y == 0 is false it evaluates x * y > 10
The following truth table matches which boolean condition?
A && ( A || B)
A || ( !A && !B)
A && ( A && B)
!A && ( A || !B)
A || ( A || B)
Consider the code:
if (a < b && c != d)
Which of the following is an example of short circuit evaluation?
if a < b is true it doesn\'t evaluate c != d
if a < b is false it doesn\'t evaluate c != d
if c != d is false it evaluates a < b
if c != d is true it doesn\'t evaluate a < b
if a < b is true it evaluates c != d
! ( x < y && w == z) is the same as which boolean expression?
x <= y && w == z
x >= y || w != z
x <= y || w != z
x <= y && w != z
x < y && w != z
Assume that x and y are boolean variables and have been properly initialized.
!(x || y) || (x || y)
The result of evaluating the expression above is best described as:
always true
always false
true only when x is true and y is true
true only when x and y have the same value
true only when x and y have different values
What is output to the screen by the following code?
int c = 2;
while (c < 6)
{
System.out.print((int)Math.pow (-1, c)+\" \");
c++;
}
-1 1 -1 1 -1 1 -1
1 -1 1 -1
-1 1 -1 1 -1 1
1 1 1 1 1 1
-1 -1 -1 -1 -1 -1
How many times will the following loop repeat?.
Which if statement below tests if letter holds R (letter is a char .pdf
1. Which if statement below tests if letter holds R? (letter is a char variable)
if ( letter == "R")
if ( letter >= 'R')
if ( letter == R)
if ( letter = 'R')
if ( letter == 'R')
What are if statements used for in programs?
Repeating commands
Storing data
Numeric calculations
Numeric casts
Making decisions
The following if statement tests the rainfall in New York’s Central Park during the months of
June, July and August.
if (low <= rain && rain <= high)
System.out.println("Rainfall amount is normal.");
else
System.out.println("Rainfall amount is abnormal.");
It could be replaced with:
I.
if (rain >= low)
{
if (rain <= high)
System.out.println("Rainfall amount is normal.");
}
else
System.out.println("Rainfall amount is abnormal.");
II.
if (rain >= low)
{
if (rain <= high)
System.out.println("Rainfall amount is normal.");
else
System.out.println("Rainfall amount is abnormal.");
}
2. else
System.out.println("Rainfall amount is abnormal.");
III.
if (rain >= low)
System.out.println("Rainfall amount is normal.");
else if (rain <= high)
System.out.println("Rainfall amount is normal.");
else
System.out.println("Rainfall amount is abnormal.");
I only
II only
III only
II or III
I, II or III
What is output by the following code?
int x = 36 % 8;
if (x >= 10)
System.out.println( 1);
else if (x >= 8)
System.out.println( 2);
else if (x >= 6)
System.out.println( 3);
else if ( x >= 4)
System.out.println( 4);
else
System.out.println( 5);
1
2
3
4
5
Consider the code:
if ( y == 0 || x * y > 10)
Which of the following is an example of short circuit evaluation?
if x * y > 10 is false it evaluates y ==0
if x * y > 10 is false it doesn't evaluate y ==0
3. if y == 0 is false it doesn't evaluate x * y > 10
if y == 0 is true it doesn't evaluate x * y > 10
if y == 0 is false it evaluates x * y > 10
The following truth table matches which boolean condition?
A && ( A || B)
A || ( !A && !B)
A && ( A && B)
!A && ( A || !B)
A || ( A || B)
Consider the code:
if (a < b && c != d)
Which of the following is an example of short circuit evaluation?
if a < b is true it doesn't evaluate c != d
if a < b is false it doesn't evaluate c != d
if c != d is false it evaluates a < b
if c != d is true it doesn't evaluate a < b
if a < b is true it evaluates c != d
! ( x < y && w == z) is the same as which boolean expression?
x <= y && w == z
x >= y || w != z
x <= y || w != z
x <= y && w != z
x < y && w != z
Assume that x and y are boolean variables and have been properly initialized.
!(x || y) || (x || y)
The result of evaluating the expression above is best described as:
always true
always false
true only when x is true and y is true
true only when x and y have the same value
true only when x and y have different values
What is output to the screen by the following code?
int c = 2;
while (c < 6)
{
System.out.print((int)Math.pow (-1, c)+" ");
4. c++;
}
-1 1 -1 1 -1 1 -1
1 -1 1 -1
-1 1 -1 1 -1 1
1 1 1 1 1 1
-1 -1 -1 -1 -1 -1
How many times will the following loop repeat?
int num = 49;
while (num > 0)
{
if (num % 2 == 0)
num++;
else
num--;
}
20
21
22
23
Infinite Loop
What is output to the screen by the following code?
int num = 1987;
while (num >0)
{
num = num / 10;
System.out.print(num % 10 + " ");
}
8 9 1 0
198 19 1 0
19 1 0 0
7 8 9 1
The loop will not terminate
The following loop is intended to print the even numbers from 20 to 26:
int x = 20;
while (x < 26)
5. {
System.out.print(x);
x++;
}
What would you need to change in order for the code to work correctly?
The x++ needs to be x += 2
The x++ needs to be x += 2 and the x < 26 needs to be <=
The x < 26 needs to be <=
It needs an if statement: if (x % 2 == 0)
Nothing, the code works as written.
The following code is intended to input three integers and print the average:
System.out.println( "Please enter three integers: ");
int a = scan.nextInt();
int b = scan.nextInt();
int c = scan.nextInt();
System.out.println( "The average is: " + 1.0 * a + b + c/3);
What is a potential problem with the code as written?
It needs ( ) so the order of operations happens correctly.
No correction needed, the code will work as written.
It should be divided by 2, not 3.
It should use scan.nextDouble instead of scan.nextInt.
The parentheses are not needed and will cause a mathematical error.
Which of the following needs a cast?
char stored in an int variable
double stored in an int variable
char stored in a String variable
int stored in a double variable
char stored in a double variable
Consider the following code segment:
int c = 1;
while (c <= 10)
{
if ( c % 3 == 1)
System.out.print(c + " ");
c++;
}
6. Which of the following produce the exact same output?
I.
int c = 1;
while (c <= 10)
{
c++;
if ( c % 3 == 1)
System.out.print(c + " ");
}
II.
int c = 1;
while (c <= 10)
{
System.out.print(c + " ");
c += 3;
}
III.
int c = 0;
while (c <= 10)
{
c++;
if ( c % 3 == 1)
System.out.print(c + " ");
}
I only
II only
III only
II and III only
I, II and III
Which of the following correctly gives random numbers between -10 and 10 inclusive?
int n = (int)(Math.random() * 20)-10;
int n = (int)(Math.random() * 21)-10;
int n = (int)(Math.random() * 11)-20;
int n = (int)(Math.random() * 10)-20;
int n = (int)(Math.random() * 10)-21;
Consider the following code:
7. int count = 4;
while (count <= 7)
{
count++;
System.out.print(count + " ");
}
What are the first and last numbers output?
4 7
4 8
5 7
5 8
Nothing is output.
Consider the following code:
int diff = 0;
if (Math.abs(num1 - num2) == (num1 - num2))
diff = num1 - num2;
else if (Math.abs(num2 - num1) == (num2 - num1))
diff = num2 - num1;
Which of the following will have the exact same result?
I.
int diff = Math.abs( num1 ) - num2;
II.
int diff = Math.abs( num1 - num2);
III.
int diff = Math.abs( num2 - num1);
I only
II only
III only
II and III only
I, II, and III
Of the following code blocks, which one correctly executes exactly two commands only when
the condition is true?
A.
B.
C.
B and C but not A
8. A and C but not BAB________111101010001
Solution
1.
The if statement which will test the condition that the letter will hold a char value ‘R’ is given as
follows:
if(letter == ‘R’)
Since, letter is a char variable. Thus, the first choice is incorrect, third and fourth are not valid if
statements. The second if statement is not checking the required condition.
Hence, the correct choice is if(letter == ‘R’).
2.
The if statements are used to make decision like structure in a program. Hence, the correct choice
is e. Making decisions.
3.
The given if statement can be replaced by nested if statements as follows:
if(rain >= low)
{
if(rain <= high)
{
System.out.println("Rainfall amount is normal.");
}
else
{
System.out.println("Rainfall amount is abnormal.");
}
}
9. else
{
System.out.println("Rainfall amount is abnormal.");
}
The above if-else structure can be explained as follows:
The firt if statement checks whether the rainfall is less than or equal to low rainfall. If this
condition satisfies, then the flow will go to the inside if block, otherwise, the else part executed.
The second if statement will check the second condition of the original if statement i.e., if(rain
<= high).
Hence, the correct choice is II only.
4.
The first statement in the given code will assign the remainder of 36/8 to the variable x. The
correct condition will be else if(x >= 4) according to the present value of x. The fourth else if
will be executeda nd prints he value 4.
Hence, the correct choice is 4.
5.
The given if statement will execute if block, when one of the two conditions is true or both
conditions are true. If the value of y is 0, then y == 0 will be true and if condition is satisfied.
Hence, the correct choice is if y == 0 is true it doesn’t evaluate x * y > 10 because x * 0 can
never be greater than 10, it will be 0.
6.
The correct boolean conditio which satisfies the given truth table is given as follows:
A || (!A && !B)
10. The output for the input A = 1 And B = 1 can be calculated as follows:
1 || (0 && 0)
1 || 0
1
The output 1 is matched with the output given in the table.
The output for the input A = 1 And B = 0 can be calculated as follows:
1 || (0 && 1)
1 || 0
1
The output 1 is matched with the output given in the table.
The output for the input A = 0 And B = 1 can be calculated as follows:
0 || (1 && 0)
0 || 0
0
The output 0 is matched with the output given in the table.
The output for the input A = 0 And B = 0 can be calculated as follows:
0 || (1 && 1)
0 || 1
1
The output 1 is matched with the output given in the table.
Hence, the correct choice is A || (!A && !B).
7.
11. The short circuit evaluation says that in case of and (&&) if first expression before && is false,
then the expression after (&&) will not be evaluated. If a < b is not satisfied, then c != d will
never be evaluated.
Hence, the correct choice is if a < b is false it doesn’t evaluate c != d.
8.
The ! operator will reverse the result of the condition (x < y && w == z). The given if condition
can be replaced by a condition in which < will be replaced by >=, && operator will be replaced
by || operator, and w == z will be replaced with !=.
Hence, the correct choice is x >= y || w != z.
9.
If (x || y) condition is true (1), then the given expression will result as true 1. This can be
explained as follows:
Assume x || y = 1
!(x || y) || (x || y)
!(1) || (1)
0 || 1 = 1
Hence, if the condition x || y is 1, then the result will also be 1 (true).
Assume x || y = 0
!(x || y) || (x || y)
!(0) || (0)
1 || 0 = 1
Hence, if the condition x || y is 0, then the result will be 1 (true).
Hence, the result will be true in each case. The correct choice is always true.
12. 10.
The output of the given code can be explained as follows:
The value of c is 2. So, the while condition c < 6 = 2 < 6 is satisfied and the while block will be
executed and the result of (-1)^2 = 1 is printed. Now, the value of c is incremented by one and
becomes 3.
The value of c is 3. So, the while condition c < 6 = 3 < 6 is satisfied and the while block will be
executed and the result of (-1)^3 = -1 is printed. Now, the value of c is incremented by one and
becomes 4.
The value of c is 4. So, the while condition c < 6 = 4 < 6 is satisfied and the while block will be
executed and the result of (-1)^4 = 1 is printed. Now, the value of c is incremented by one and
becomes 5.
The value of c is 5. So, the while condition c < 6 = 5 < 6 is satisfied and the while block will be
executed and the result of (-1)^5 = -1 is printed. Now, the value of c is incremented by one and
becomes 6.
The value of c is 6. So, the while condition c < 6 = 6 < 6 is not satisfied and the while block is
not executed. The program stops and the required output on the console will be 1 -1 1 -1.
Hence, the correct choice is 1 -1 1 -1.
11.
The execution of given while loop can be explained as follows:
The value of num is 49 initially and the condition in the while num > 0 is satisfied. The while
loop is executed and the if condition num % 2 == 0 is not satisfied. The else part executed and
value of num is decremented by one and becomes 48.
The value of num is 48 and the condition in the while num > 0 is satisfied. The while loop is
executed and the if condition num % 2 == 0 is satisfied. The value of num is incremented by one
13. and becomes 49 again.
This value of num will become 48 and 49 alternatively and the loop will never ends.
Hence, the correct choice is infinite loop.
12.
The output of the given code can be explaias follows:
The value of num is 1987 initially. The condition of while num > 0 is satisfied and the while
block is executed. The num becomes num / 10 i.e., 1987 / 10 = 198 and the value of 198 % 10 =
8 is printed.
The value of num is 198. The condition of while num > 0 is satisfied and the while block is
executed. The num becomes num / 10 i.e., 198 / 10 = 19 and the value of 19 % 10 = 9 is printed.
The value of num is 19. The condition of while num > 0 is satisfied and the while block is
executed. The num becomes num / 10 i.e., 19 / 10 = 1 and the value of 1 % 10 = 1 is printed.
The value of num is 1. The condition of while num > 0 is satisfied and the while block is
executed. The num becomes num / 10 i.e., 1 / 10 = 0 and the value of 0 % 10 = 0 is printed.
The value of num is 0. The condition of while num > 0 is not satisfied and the program stops.
The required output will be 8 9 1 0.
Hence, the correct choice is 8 9 1 0.
13.
The present code will print the numbers from 20 to 25. The modifications in the code to print
even numbers from 20 to 26 is shown as follows:
The condition in the while x < 26 needs to be replaced with x <= 26 otherwise, 26 will not be
printed. The increment of x in the while loop should be changed such that x is incremented by 2
in each iteration.
14. Initially in the while loop x = 20 willl be printed and now x becomes 22 by x+ = 2. Now the
value 22 is printed and value of x becomes 24 and so on.
Hence, the correct choice is that the x++ needs to be x += 2 and the x < 26 needs to be x <= 26.
14.
There is an logical error in the last statement of the code. It will not give correct average of three
numbers because the sum of three numbers is not divided by 3 instead, division of third number
by 3 is added to the sum of first two numbers.
The whole sum should be divided by 3 to get the correct average. The parentheses should be
added to the statement a + b + c/3 like (a + b + c)/3 and now the correct average value will be
printed.
Hence, the correct choice is that it needs () so the order of operations happens correctly.
15.
The type casting is used to change the data type of the right hand side value to match with the
data type of the left hand side value. If a double value is stored in a int variable, then it needs
type casting to change the double to int.
int a = (int)8.5;
Hence, the correct choice is double stored in the an int variable.
16.
The code with the same output of the given code will be as follows:
The II code will generate same output because the output values are generated by the gap of 3.
The output will be 1, 4, 7, 10. The III code will also give same values because it increments the
value of c first and then prints the value of c if c % 3 == 1.
15. Hence, the correct choice is II and III only.
17.
The correct stratement which will generate a random value between -10 and 10 inclusively is
shown as follows:
int n = (int)(Math.random() * 20) – 10;
The above statement will select a random number between -10 to 10 because Math.random() *
20 will generate a random number less than 20 and (Math.random() * 20) -1 will generate a
random number -10 to 10.
Hence, the correct choice is int n = (int)(Math.random() * 20) – 10;.
18.
The first number which is printed by the given code will be 5 because the condition in the while
loop is satisfied and value of count becomes 5 by the statement count++ in the while loop.
The value of count is incremented in each iteration till it becomes greater than 7. So, the last
value will be 8.
Hence, the correct choice is 5 8.
19.
The given code will always print the positive value of variable diff because if the first condition
in the if statement satisfied, then the difference will be num1 – num2 and it will be positive
otherwise, num2 – num1 and it is also positive.
This if-else if structure can be replaced by the statements II and III becausboth statements will
give same output as of given code.
Hence, the correct choice is II and III only.
16. 20.
The B code is not correct because there is a need of curly bracket block for the if statement as
there are two statements in the if block. The A and C code will give two commands.
Hence, the correct choice is A and C but not B.