download instant at

Name:______
Covers Chapter 4 / CSCI 1301 Introduction to Programming
ArmstrongAtlanticStateUniversity
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 ______.

PartI.

(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) + " ");

}

}

(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: (10 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. Write a loop that computes (No need to write a complete program)

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

#

2Analyze 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

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;
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

Please double check your answer before clicking the Submit button. Whatever submitted to LiveLab is FINAL and counted for your grade.

Have you submitted your answer to LiveLib? ______

What is your score? ______

download instant at