(A) (3 Pts) If You Enter Input 9, Show the Printout of the Following Code

Total Page:16

File Type:pdf, Size:1020Kb

(A) (3 Pts) If You Enter Input 9, Show the Printout of the Following Code

Name:______CSCI 1301 Introduction to Programming Covers Chapter 4 Armstrong Atlantic State University Instructor: Y. Daniel Liang

I pledge by honor that I will not discuss this exam with anyone until my instructor reviews the exam in the class. Signed by ______.

Part I. (a) (3 pts) If you enter input 9, show the printout of the following code:

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"); } }

(b) (2 pts) public class Test { public static void main(String[] args) { for (int i = 0; i < 5; i++) System.out.print((i + 4) + " "); } }

1 (c) (4 pts) public class Test { public static void main(String[] args) { int i = 1; while (i <= 4) { int num = 1; for (int j = 1; j <= i; j++) { System.out.print(num + "bb"); num *= 3; }

System.out.println(); i++; } } }

Part II: (8 pts each):

1. Write a complete program that prints numbers from 1 to 50, but if numbers that are multiples of 5, print HiFive, else if numbers that are divisible by 2, print HiTwo, and else if numbers that are divisible by 3 or 7, print HiThreeOrSeven.

2 2. Write a loop that computes (No need to write a complete program)

1 2 3 98 99    ...   2 3 4 99 100

3 Part III: Multiple Choice Questions: (1 pts each) (1. Mark your answers on the sheet. 2. Login and click Take Instructor Assigned Quiz for Quiz2. 3. Submit it online within 5 mins. 4. Close the Internet browser.)

1 What is the output for y? int y = 0; for (int i = 0; i<10; ++i) { y += i; } System.out.println(y); a. 45 b. 11 c. 13 d. 12 e. 10

#

2 Analyze the following code.

int x = 1; while (0 < x) && (x < 100) System.out.println(x++); a. The loop runs forever. b. The code does not compile because the loop body is not in the braces. c. The code does not compile because (0 < x) && (x < 100) is not enclosed in a pair of parentheses. d. The numbers 1 to 99 are displayed. e. The numbers 2 to 100 are displayed.

#

3 How many times will the following code print "Welcome to Java"? int count = 0; do { System.out.println("Welcome to Java"); count++; } while (count < 10);

A. 8 B. 0 C. 11 D. 10

4 E. 9

#

4 What is i after the following for loop is finished? int y = 0; for (int i = 0; i<10; ++i) { y += i; }

A. 10 B. undefined C. 9 D. 11

#

5 Analyze the following code: public class Test { public static void main (String args[]) { int i = 0; for (i = 0; i < 10; i++); System.out.println(i + 4); } }

A. The program compiles despite the semicolon (;) on the for loop line, and displays 4. B. The program compiles despite the semicolon (;) on the for loop line, and displays 14. C. The program has a compile error because of the semicolon (;) on the for loop line. D. The program has a runtime error because of the semicolon (;) on the for loop line.

#

6 Which of the following expression yields an integer between 0 and 100, inclusive? A. (int)(Math.random() * 100 + 1) B. (int)(Math.random() * 101) C. (int)(Math.random() * 100) D. (int)(Math.random() * 100) + 1

#

7 Analyze the following code. int count = 0;

5 while (count < 100) { // Point A System.out.println("Welcome to Java!"); count++; // Point B }

// Point C

A. count < 100 is always false at Point A B. count < 100 is always true at Point B C. count < 100 is always false at Point C D. count < 100 is always true at Point C E. count < 100 is always false at Point B

#

8. What is the output of the following fragment? int i = 1; int j = 1; while (i < 5) { i++; j = j * 2; } System.out.println(j); a. 4 b. 8 c. 16 d. 32 e. 64

6 Solution:

Part I. (a) i is 4 9 is not prime

(b) 4 5 6 7 8

(c) 1bb 1bb3bb 1bb3bb9bb 1bb3bb9bb27bb

Part II:

(a)

public class Test { public static void main(String[] args) { for (int i = 1; i <= 50; i++) if (i % 5 == 0) System.out.println("HiFive"); else if (i % 2 == 0) System.out.println("HiTwo"); else if (i % 3 == 0 || i % 7 == 0) System.out.println("HiThreeOrSeven"); else System.out.println(i + " "); } }

(b)

public class Test { /**Main method*/ public static void main(String[] args) { double sum = 0;

for (int i = 1; i <= 99; i++) sum += (i * 1.0 / (i + 1));

// Display results System.out.println("Sum is " + sum); } }

1. What is the output for y? int y = 0;

7 for (int i = 0; i<10; ++i) { y += i; } System.out.println(y); a. 45 b. 11 c. 13 d. 12 e. 10 Key:a

#

2. Analyze the following code.

int x = 1; while (0 < x) && (x < 100) System.out.println(x++); a. The loop runs forever. b. The code does not compile because the loop body is not in the braces. c. The code does not compile because (0 < x) && (x < 100) is not enclosed in a pair of parentheses. d. The numbers 1 to 99 are displayed. e. The numbers 2 to 100 are displayed. Key:c

#

3. How many times will the following code print "Welcome to Java"? int count = 0; do { System.out.println("Welcome to Java"); count++; } while (count < 10);

A. 8 B. 0 C. 11 D. 10 E. 9 Key:d

#

8 4. What is i after the following for loop is finished? int y = 0; for (int i = 0; i<10; ++i) { y += i; }

A. 10 B. undefined C. 9 D. 11 Key:b

#

5. Analyze the following code: public class Test { public static void main (String args[]) { int i = 0; for (i = 0; i < 10; i++); System.out.println(i + 4); } }

A. The program compiles despite the semicolon (;) on the for loop line, and displays 4. B. The program compiles despite the semicolon (;) on the for loop line, and displays 14. C. The program has a compile error because of the semicolon (;) on the for loop line. D. The program has a runtime error because of the semicolon (;) on the for loop line. Key:b

#

6. Which of the following expression yields an integer between 0 and 100, inclusive? A. (int)(Math.random() * 100 + 1) B. (int)(Math.random() * 101) C. (int)(Math.random() * 100) D. (int)(Math.random() * 100) + 1 Key:b

#

7. Analyze the following code. int count = 0; while (count < 100) { // Point A

9 System.out.println("Welcome to Java!"); count++; // Point B }

// Point C

A. count < 100 is always false at Point A B. count < 100 is always true at Point B C. count < 100 is always false at Point C D. count < 100 is always true at Point C E. count < 100 is always false at Point B Key:c

#

8. What is the output of the following fragment? int i = 1; int j = 1; while (i < 5) { i++; j = j * 2; } System.out.println(j); a. 4 b. 8 c. 16 d. 32 e. 64 Key:c

10

Recommended publications