Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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:

1)

Code 1 has compile errors.

2)

Code 2 has compile errors.

3)

Both Code 1 and Code 2 have compile errors.

4)

Both Code 1 and Code 2 are correct, but Code 2 is better.

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:

1)

I

2)

II

3)

III

4)

IV

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:

1)

I

2)

II

3)

III

4)

IV

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:

1)

x > 9 and y > 8;

2)

x <= 9 and z >= 7;

3)

x <= 9 and z < 7;

4)

none

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:

1)

number is always printed out at least once;

2)

number is printed out twice if number is zero;

3)

number is printed out twice if number is negative;

4)

number is printed out once if number is positive.

Question 10 (1 point)

Analyze the following code. int x = 0; if (x > 0); { System.out.println("x"); }

Question 10 options:

1)

The symbol x is always printed.

2)

The value of variable x is always printed.

3)

Nothing is printed because x > 0 is false.

4)

The symbol x is always printed twice.

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:

1)

in case cond1 is true and cond2 is false

2)

in case cond1 is true and cond2 is true

3)

in case cond1 is false and cond2 is false

4)

in case cond1 is false and cond2 is true

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:

1)

in case cond1 is true and cond2 is false

2)

in case cond1 is true and cond2 is true

3)

in case cond1 is false and cond2 is false

4)

in case cond1 is false and cond2 is true

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:

1)

The program has a syntax error

2)

The program has a runtime error

3)

The program displays "It is odd!"

4)

The program displays "It is even!"

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:

1)

x is greater than 0

2)

x is less than 0

3)

x equals 0

4)

None

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:

1)

no output

2)

Income is greater than 3000

3)

Income is greater than 3000 followed by Income is greater than 4000

4)

Income is greater than 4000

5)

Income is greater than 4000 followed by Income is greater than 3000

Question 16 (1 point)

Analyze the following code:

boolean even = false; if (even = true) { System.out.println("It is even!"); }

Question 16 options:

1)

The program has a compile error.

2)

The program has a runtime error.

3)

The program runs fine, but displays nothing.

4)

The program runs fine and displays It is even!.

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Logic In Databases International Workshop Lid 96 San Miniato Italy July 1 2 1996 Proceedings Lncs 1154

Authors: Dino Pedreschi ,Carlo Zaniolo

1st Edition

3540618147, 978-3540618140

More Books

Students also viewed these Databases questions

Question

Develop a program for effectively managing diversity. page 303

Answered: 1 week ago

Question

List the common methods used in selecting human resources. page 239

Answered: 1 week ago