Exam 2 CSE 174 ACTUAL UPDATED QUESTIONS AND CORRECT ANSWERS
Consider the following code snippet: if (number > 30) {
int number = 0;
Scanner in = new Scanner(System.in);
System.out.print("Enter a number: "); number = in.nextInt();
if (number > 30) { . . . }
else if (number > 20) { ....... }
else if (number > 10) { ...... }
else {...... }
Which of the following options is a legally correct if (!a == 10)
expression for inverting a condition?
if (!(a == 10))
if (!a == 10)
if (a ! 10)
if (a !== 10)
What is the output of the following code snippet? his
String someString1 = "his";
String someString2 = "cycle";
if (someString1.compareTo(someString2) < 0) {
System.out.println(someString2);
}
else { System.out.println(someString1);
}
,Which of the following statements is true about the if The else block is optional
statement?
The else block is optional.
The if statement can have only one condition that
evaluates to an integer value.
The if and else blocks should always be included within
curly braces.
The if block is optional.
if (cost > 100); { Logical error: if statement has do-nothing statement after if condition
cost = cost - 10;
}
System.out.println("Discount cost: " + cost);
Syntax error (won't compile)
Logical error: if statement has do-nothing statement after
if condition
Logical error: use of an uninitialized variable
Logical error: assignment statement does not show
equality
Consider the following code snippet. What is the Using == to test the double variable average for equality is error-prone.
potential problem with the if statement?
double average; average = (g1 + g2 + g3 + g4) / 4.0;
if (average == 90.0) {
System.out.println("You earned an A in the class!");
}
Literals should never be used in if statement conditionals.
The assignment operator should not be used within an if-
statement conditional.
The conditional will not evaluate to a Boolean value.
Using == to test the double variable average for equality
is error-prone.
,Which of the following is the correct syntax for an if if (x < 10) { size = "Small"; }
statement? else { size = "Medium"; }
if (x < 10) { size = "Small"; }
else { size = "Medium"; }
if (x < 10) { size = "Small"; } else (x < 20) { size = "Medium"; }
if { size = "Small"; }
else (x < 20) { size = "Medium"; }
if (x < 10); { size = "Small"; }
else (x < 20) { size = "Medium"; }
Which of the following variables is used to store a Boolean
condition that can be either true or false?
Algebraic
Logical
Conditional
Boolean
In Java, which of the following orderings is used to Lexicgographic
compare strings?
Semantic
Lexicographic
Alphabetic
Syntactic
Which of the following operators compare using short- &&
circuit evaluation?
++
-
==
&&
, What is the output of the following code snippet if the 24
input is 25?
int i = 0;
Scanner in = new Scanner(System.in);
System.out.print("Enter a number: ");
i = in.nextInt();
if (i > 25) {
i++;
} else {
i--;
} System.out.print(i);
26
25
27
24
An if statement inside another if statement is called a nested if statement
_________________.
nested if statement
syntax error, since that is not permitted in Java
break statement
switch statement
Which of the following operators is used to combine two &&
Boolean conditions?
&&
##
$$
%%
Consider the following code snippet: if (number > 30) {
int number = 0;
Scanner in = new Scanner(System.in);
System.out.print("Enter a number: "); number = in.nextInt();
if (number > 30) { . . . }
else if (number > 20) { ....... }
else if (number > 10) { ...... }
else {...... }
Which of the following options is a legally correct if (!a == 10)
expression for inverting a condition?
if (!(a == 10))
if (!a == 10)
if (a ! 10)
if (a !== 10)
What is the output of the following code snippet? his
String someString1 = "his";
String someString2 = "cycle";
if (someString1.compareTo(someString2) < 0) {
System.out.println(someString2);
}
else { System.out.println(someString1);
}
,Which of the following statements is true about the if The else block is optional
statement?
The else block is optional.
The if statement can have only one condition that
evaluates to an integer value.
The if and else blocks should always be included within
curly braces.
The if block is optional.
if (cost > 100); { Logical error: if statement has do-nothing statement after if condition
cost = cost - 10;
}
System.out.println("Discount cost: " + cost);
Syntax error (won't compile)
Logical error: if statement has do-nothing statement after
if condition
Logical error: use of an uninitialized variable
Logical error: assignment statement does not show
equality
Consider the following code snippet. What is the Using == to test the double variable average for equality is error-prone.
potential problem with the if statement?
double average; average = (g1 + g2 + g3 + g4) / 4.0;
if (average == 90.0) {
System.out.println("You earned an A in the class!");
}
Literals should never be used in if statement conditionals.
The assignment operator should not be used within an if-
statement conditional.
The conditional will not evaluate to a Boolean value.
Using == to test the double variable average for equality
is error-prone.
,Which of the following is the correct syntax for an if if (x < 10) { size = "Small"; }
statement? else { size = "Medium"; }
if (x < 10) { size = "Small"; }
else { size = "Medium"; }
if (x < 10) { size = "Small"; } else (x < 20) { size = "Medium"; }
if { size = "Small"; }
else (x < 20) { size = "Medium"; }
if (x < 10); { size = "Small"; }
else (x < 20) { size = "Medium"; }
Which of the following variables is used to store a Boolean
condition that can be either true or false?
Algebraic
Logical
Conditional
Boolean
In Java, which of the following orderings is used to Lexicgographic
compare strings?
Semantic
Lexicographic
Alphabetic
Syntactic
Which of the following operators compare using short- &&
circuit evaluation?
++
-
==
&&
, What is the output of the following code snippet if the 24
input is 25?
int i = 0;
Scanner in = new Scanner(System.in);
System.out.print("Enter a number: ");
i = in.nextInt();
if (i > 25) {
i++;
} else {
i--;
} System.out.print(i);
26
25
27
24
An if statement inside another if statement is called a nested if statement
_________________.
nested if statement
syntax error, since that is not permitted in Java
break statement
switch statement
Which of the following operators is used to combine two &&
Boolean conditions?
&&
##
$$
%%