Sunteți pe pagina 1din 4

1.

What will print if the


following Java code is Mark for Review
executed? (1) Points

if ((5.1 > 4.3 && 6.2 <


8.4) && !(7.2 < 3.5 || 1.2
== 2.1 || 2.2 != 2.25))
System.out.print("TRUE");
else
System.out.print("FALSE");
True

False (*)

Correct

2. In an if-else construct the condition to be evaluated must end


with a semi-colon. True or false? Mark for Review
(1) Points
True

False (*)

Correct

3. The six relational operators in Java are:


Mark for Review
(1) Points
>,<,=,!,<=,>=

>,<,=,!=,<=,>=

>,<,=,!=,=<,=>

>,<,==,!=,<=,>= (*)

Correct

4. The following code fragment properly implements the switch


statement. True or false? Mark for Review
(1) Points
default(input)
switch '+':
answer+=num;
break;
case '-':
answer-=num;
break;
!default
System.out.println("Invalid input");
True

False (*)

Correct

5. Which of the following could be a reason to use a switch


statement in a Java program? Mark for Review
(1) Points
Because it allows the code to be run through until a certain
conditional statement is true.
Because it allows the user to enter an input in the console
screen and prints out a message that the user input was
successfully read in.
Because it allows the program to run certain segments of
code and neglect to run others based on the input given.
(*)
Because it terminates the current loop.

Correct

6. How would you use the


ternary operator to Mark for Review
rewrite this if (1) Points
statement?

if (gender == "female")
System.out.print("Ms.");
else
System.out.print("Mr.");
System.out.print( (gender == "female") ? "Mr." : "Ms." );

System.out.print( (gender == "female") ? "Ms." : "Mr." ); (*)

(gender == "female") ? "Ms." : "Mr." ;

(gender == "female") ? "Mr." : "Ms." ;

Correct

7. Consider that a Scanner has been initialized such that:


Mark for Review
Scanner in = new Scanner(System.in); (1) Points

Which of the following lines of code reads in the users input and
sets it equal to a new String called input?
String input = in.close();

String input = in.nextInt();

String input = in.next(); (*)

String input = new String in.next();

Correct

8. Which of the following expressions will evaluate to true when x


and y are boolean variables with opposite values? Mark for Review
(1) Points
I. (x || y) && !(x && y)
II. (x && !y) || (!x && y)
III. (x || y) && (!x ||!y)
I only

II only
I and III

II and III

I, II, and III (*)

Correct

9. A counter used in a for loop cannot be initialized within the For


loop header. True or false? Mark for Review
(1) Points
True

False (*)

Correct

10. Updating the input of a loop allows you to implement the code
with the next element rather than repeating the code always with Mark for Review
the same element. True or false? (1) Points

True (*)

False

Correct
11. What is the output of the
following code segment? Mark for Review
(1) Points
int num = 7;
while(num >= 0)
{
num -= 3;
}
System.out.println(num);
0

-2 (*)

Correct

12. One advantage to using a while loop over a for loop is that a
while loop always has a counter. True or false? Mark for Review
(1) Points
True

False (*)

Correct

13. What is the function of the word "break" in Java?


Mark for Review
(1) Points
It stops the program from running.
It exits the current loop or case statement. (*)

It continues onto the next line of code.

It does not exist in Java.

Correct

14. When the for loop condition statement is met the construct is
exited. True or false? Mark for Review
(1) Points
True

False (*)

Incorrect. Refer to Section 5 Lesson


2.
15. What is one significant difference between a while loop and a
do-while loop? Mark for Review
(1) Points
There is no difference between a DO-WHILE loop and a
WHILE loop.
A DO-WHILE loop will always execute the code at least
once, even if the conditional statement for the WHILE is
never true. A WHILE loop is only executed if the conditional
statement is true. (*)
A DO-WHILE loop does not exist in Java and a WHILE loop
does.
A DO-WHILE loop includes an int that serves as a counter
and a WHILE loop does not.
Incorrect. Refer to Section 5 Lesson
2.

S-ar putea să vă placă și