Question
Analyze the following code: Code 1: int number = 45; boolean even; if (number % 2 == 0) even = true; else even = false;
Analyze the following code:
Code 1: int number = 45; boolean even; if (number % 2 == 0) even = true; else even = false;
Code 2: int number = 45; boolean even = (number % 2 == 0);
Question 5 options:
|
| ||
|
| ||
|
| ||
|
|
Question 6 (1 point)
Suppose you write the code to display "Cannot get a driver's license" if age is less than 16 and "Can get a driver's license" if age is greater than or equal to 16.
Which of the following code is correct? (multiple correct answers)
I: if (age < 16) System.out.println("Cannot get a driver's license"); if (age >= 16) System.out.println("Can get a driver's license");
II: if (age < 16) System.out.println("Cannot get a driver's license"); else System.out.println("Can get a driver's license");
III: if (age < 16) System.out.println("Cannot get a driver's license"); else if (age >= 16) System.out.println("Can get a driver's license");
IV: if (age < 16) System.out.println("Cannot get a driver's license"); else if (age > 16) System.out.println("Can get a driver's license"); else if (age == 16) System.out.println("Can get a driver's license");
Question 6 options:
|
| ||
|
| ||
|
| ||
|
|
Question 7 (1 point)
Suppose you write the code to display "Cannot get a driver's license" if age is less than 16 and "Can get a driver's license" if age is greater than or equal to 16.
Which of the following code is the best? (only one best answer)
I: if (age < 16) System.out.println("Cannot get a driver's license"); if (age >= 16) System.out.println("Can get a driver's license");
II: if (age < 16) System.out.println("Cannot get a driver's license"); else System.out.println("Can get a driver's license");
III: if (age < 16) System.out.println("Cannot get a driver's license"); else if (age >= 16) System.out.println("Can get a driver's license");
IV: if (age < 16) System.out.println("Cannot get a driver's license"); else if (age > 16) System.out.println("Can get a driver's license"); else if (age == 16) System.out.println("Can get a driver's license");
Question 7 options:
|
| ||
|
| ||
|
| ||
|
|
Question 8 (1 point)
What is the output of the following code: (Please indent the statement correctly first.) Hint: The else always matches the closest if above.
int x = 9; int y = 8; int z = 7; if (x > 9) if (y > 8) System.out.println("x > 9 and y > 8"); else if (z >= 7) System.out.println("x <= 9 and z >= 7"); else System.out.println("x <= 9 and z < 7");
Question 8 options:
|
| ||
|
| ||
|
| ||
|
|
Question 9 (1 point)
Analyze the following code:
// Enter an integer Scanner input = new Scanner(System.in); int number = input.nextInt(); if (number <= 0) System.out.println(number); System.out.println(number);
Question 9 options:
|
| ||
|
| ||
|
| ||
|
|
Question 10 (1 point)
Analyze the following code. int x = 0; if (x > 0); { System.out.println("x"); }
Question 10 options:
|
| ||
|
| ||
|
| ||
|
|
Question 11 (1 point)
Suppose cond1 and cond2 are two Boolean expressions. When will this if condition be true?
if (cond1 && cond2) ...
Question 11 options:
|
| ||
|
| ||
|
| ||
|
|
Question 12 (1 point)
Suppose cond1 and cond2 are two Boolean expressions. When will this if condition be true?
if (cond1 || cond2) ...
Question 12 options:
|
| ||
|
| ||
|
| ||
|
|
Question 13 (1 point)
Analyze the following code:
boolean even = ((231 % 2) == 0); if (even = true) System.out.println("It is even!"); else System.out.println("It is odd!");
Question 13 options:
|
| ||
|
| ||
|
| ||
|
|
Question 14 (1 point)
Assume x is 0. What is the output of the following statement?
if (x > 0) System.out.print("x is greater than 0"); else if (x < 0) System.out.print("x is less than 0"); else System.out.print("x equals 0");
Question 14 options:
|
| ||
|
| ||
|
| ||
|
|
Question 15 (1 point)
Suppose income is 4001, what is the output of the following code:
if (income > 3000) { System.out.println("Income is greater than 3000"); } else if (income > 4000) { System.out.println("Income is greater than 4000"); }
Question 15 options:
|
| ||
|
| ||
|
| ||
|
| ||
|
|
Question 16 (1 point)
Analyze the following code:
boolean even = false; if (even = true) { System.out.println("It is even!"); }
Question 16 options:
|
| ||
|
| ||
|
| ||
|
|
Question 17 (1 point)
Analyze the following code:
if (x < 100) && (x > 10) System.out.println("x is between 10 and 100");
Question 17 options:
| The statement has compile errors because (x<100) & (x > 10) must be enclosed inside parentheses. |
| The statement has compile errors because (x<100) & (x > 10) must be enclosed inside parentheses and the println() statement must be put inside a block. |
| The statement compiles fine. |
| The statement compiles fine, but has a runtime error. |
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started