Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Section 5 Using Scanner and Conditional

Statements
Test: Using Scanner and Conditional
Statements: Quiz
Review your answers, feedback, and question scores below. An asterisk (*) indicates a
correct answer.

Section 1
(Answer all questions in this section)
1.What will print if the following Java code is executed?
Mark for
Review
(1) Points

0
3 (*)
4
5

Incorrect. Refer to Section 5 Lesson 1.


2.Which of the following correctly initializes an instance of Scanner, called
Mark for
"in", that reads input from the console screen?
Review
(1) Points
Scanner in = new Scanner(System.in); (*)
Scanner in = new Scanner("System.in");
Scanner in = Scanner(System.in);
System.in in = new Scanner();

Incorrect. Refer to Section 5 Lesson 1.


3.Which of the two diagrams below illustrate the correct syntax for variables
Mark for
used in an if-else statement?
Review
(1) Points
Example A (*)
Example B

Incorrect. Refer to Section 5 Lesson 1.


4.In an if-else construct, the condition to be evaluated must be contained
Mark for
within parentheses. True or False?
Review
(1) Points
True (*)
False

Correct
5.The six relational operators in Java are:
Mark for
Review
(1) Points
>,<,=,!,<=,>=
>,<,=,!=,=<,=>
>,<,=,!=,<=,>=
>,<,==,!=,<=,>= (*)
Incorrect. Refer to Section 5 Lesson 1.
6.Determine whether this boolean expression evaluates to true or false:
Mark for
!(3<4&&5>6||6<=6&&7-1==6)
Review
(1) Points
True
False (*)

Incorrect. Refer to Section 5 Lesson 1.


7.The three logic operators in Java are:
Mark for
Review
(1) Points
!=,=,==
&&, ||, ! (*)
&&,!=,=
&,|,=

Incorrect. Refer to Section 5 Lesson 1.


8.What is the difference between the symbols = and == ?
Mark for
Review
(1) Points
The symbol = is used in if statements and == is used in loops.
The symbol == is used to assign values to variables and the = is used
in declarations.
The = is use to assign values to variables and the == compares values.
(*)
There is no difference.

Incorrect. Refer to Section 5 Lesson 1.


9.In Java, each case seqment of a switch statement requires the keyword
Mark for
break to avoid "falling through".
Review
(1) Points
True (*)
False

Correct
1The following code fragment properly implements the switch statement.
Mark for
0.True or false?
Review
(1) Points
default(input)
switch '+':
answer+=num;
break;
case '-':
answer-=num;
break;
!default
System.out.println("Invalid input");
True
False (*)

Incorrect. Refer to Section 5 Lesson 1.


1How would you use the ternary operator to rewrite this if statement?
Mark for
1.
Review
if (skillLevel > 5)
(1) Points
numberOfEnemies = 10;
else
numberOfEnemies = 5;
numberOfEnemies = ( skillLevel > 5) ? 5 : 10;
numberOfEnemies = ( skillLevel < 5) ? 10 : 5;
numberOfEnemies = ( skillLevel >= 5) ? 5 : 10;
numberOfEnemies = ( skillLevel >= 5) ? 10 : 5;
numberOfEnemies = ( skillLevel > 5) ? 10 : 5; (*)

Incorrect. Refer to Section 5 Lesson 1.


1How would you use the ternary operator to rewrite this if statement?
Mark for
2.
Review
if (gender == "male")
(1) Points
System.out.print("Mr.");
else
System.out.print("Ms.");
System.out.print( (gender == "male") ? "Mr." : "Ms." ); (*)
System.out.print( (gender == "male") ? "Ms." : "Mr." );
(gender == "male") ? "Mr." : "Ms." ;
(gender == "male") ? "Ms." : "Mr." ;

Correct

Page 1 of 1

You might also like