Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

With a Scanner object created as follows, what method do you use to read an int value? Scanner input = new Scanner(System.in); A. input.int(); B.

With a Scanner object created as follows, what method do you use to read an int value?

Scanner input = new Scanner(System.in);

A.

input.int();

B.

input.nextInt();

C.

input.integer();

D.

input.nextInteger();

What is the output of the following code?

x = 0; if (x > 0) System.out.println("x is greater than 0"); else if (x < 0) System.out.println("x is less than 0"); else System.out.println("x equals 0");

A.

x is greater than 0

B.

x is less than 0

C.

x equals 0

D.

None

The following loop displays ________.

for (int i = 1; i <= 10; i++) { System.out.print(i + " "); i++ }

A.

1 3 5 7 9

B.

1 2 3 4 5

C.

1 2 3 4 5 6 7 8 9 10

D.

1 2 3 4 5 6 7 8 9

The main method header is written as ________.

A.

public static void Main(String[] args)

B.

public static void main(String[] args)

C.

public static void main(string[] args)

D.

public void main(String[] args)

What is y after the following switch statement is executed?

int x = 3; int y = 4; switch(x + 3) { case 6: y = 0; case 7: y = 1; default: y += 1; }

A.

0

B.

4

C.

1

D.

2

Which of the following is the best for generating random integer 0 or 1?

A.

(int)(Math.random() + 0.5)

B.

(int)Math.random()

C.

(int)(Math.random() + 0.2)

D.

(int)Math.random() + 1

What is the printout of the following code?

double x = 5.5; int y = (int)x; System.out.println("x is " + x + " and y is " + y);

A.

x is 6 and y is 6

B.

x is 5.5 and y is 5.0

C.

x is 5.5 and y is 5

D.

x is 6.0 and y is 6.0

Math.pow(2, 3) returns ________.

A.

9.0

B.

8

C.

9

D.

8.0

What is the printout of the following code:

double x = 10.1; int y = (int)x; System.out.println("x is " + x + " and y is " + y);

A.

x is 10.1 and y is 10.0

B.

x is 10 and y is 10

C.

x is 10.1 and y is 10

D.

x is 10.0 and y is 10.0

Analyze the following code:

if (x < 100) && (x > 10) System.out.println("x is between 10 and 100");

A.

The statement has compile errors because (x < 100) && (x > 10) must be enclosed inside parentheses.

B.

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.

C.

The statement compiles fine, but has a runtime error.

D.

The statement compiles fine.

What is the value in count after the following loop is executed?

int count = 0; do { System.out.println("Welcome to Java"); } while (count++ < 9);

A.

10

B.

11

C.

0

D.

9

Which of the following operators has the highest precedence?

A.

*

B.

/

C.

+

D.

casting

The not equal comparison operator in Java is ________.

A.

^=

B.

<>

C.

!=

D.

!= =

Suppose x = 1, y = -1, and z = 1. What is the printout of the following code?

if (x < 0) if (y < 0) System.out. println("x < 0 and y < 0"); else if (z < 0) System.out. println("x < 0 and z < 0");

else

System.out. println("x < 0 and z > 0");

A.

x > 0 and y > 0;

B.

x < 0 and z > 0;

C.

x < 0 and z < 0;

D.

no printout.

Suppose the input for number is 9. What is the output from running the following program?

import java. util.Scanner; public class Test { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter an integer: "); int number = input. nextInt(): int i; boolean isPrime = true; for (i = 2; i < number && isPrime; i++) { if (number % i == 0) isPrime = false; } System.out. println("i is " + i);

System.out. println(number + " is" ( isPrime ? "" : " not") + " prime"); }

A.

i is 3 followed by 9 is prime

B.

i is 3 followed by 9 is not prime

C.

i is 4 followed by 9 is prime

D.

i is 4 followed by 9 is not prime

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

More Books

Students also viewed these Databases questions