Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Document preview thumbnail
Preview 3 out of 21 pages
Exam (elaborations)

Java - Chapter 5 Quiz – Questions and Answers| Latest Update

Document preview thumbnail
Preview 3 out of 21 pages

Java - Chapter 5 Quiz – Questions and Answers| Latest Update

Content preview

Java - Chapter 5 Quiz – Questions and
Answers| Latest Update
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);


if (isPrime)
System.out.println(number + " is prime");
else

,System.out.println(number + " is 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 ✔️✔️D


Analyze the following code:


import java.util.Scanner;


public class Test {
public static void main(String[] args) {
int sum = 0;
for (int i = 0; i < 100000; i++) {
Scanner input = new Scanner(System.in);
sum += input.nextInt();
}
}
}
A. The program does not compile because the Scanner input = new Scanner(System.in);
statement is inside the loop.
B. The program compiles, but does not run because the Scanner input = new
Scanner(System.in); statement is inside the loop.
C. The program compiles and runs, but it is not efficient and unnecessary to execute the
Scanner input = new Scanner(System.in); statement inside the loop. You should move the
statement before the loop.

, D. The program compiles, but does not run because there is not prompting message for
entering the input ✔️✔️C


What is the output of the following code?


int x = 0;
while (x < 4) {
x = x + 1;
}
System.out.println("x is " + x);
A. x is 0
B. x is 1
C. x is 2
D. x is 3

E. x is 4 ✔️✔️E


What will be displayed when the following code is executed?


int number = 6;
while (number > 0) {
number -= 3;
System.out.print(number + " ");
}
A. 6 3 0
B. 6 3
C. 3 0
D. 3 0 -3

Document information

Uploaded on
March 21, 2025
Number of pages
21
Written in
2024/2025
Type
Exam (elaborations)
Contains
Questions & answers
$12.99

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Seller avatar
Reputation scores are based on the amount of documents a seller has sold for a fee and the reviews they have received for those documents. There are three levels: Bronze, Silver and Gold. The better the reputation, the more your can rely on the quality of the sellers work.
KateJones
4.8
(12)
Sold
31
Followers
0
Items
5707
Last sold
1 week ago




Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions