Name:_______________________



Name:_______________________

Covers Chapter 4

50 mins (Practice Exam) |CSCI 1301 Introduction to Programming

Armstrong Atlantic State University

Instructor: Y. Daniel Liang | |

(Open book test, you can only bring the textbook)

Part I: Multiple Choice Questions.

A question may have multiple answers:

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

System.out.println(count);

A. 9

B. 8

C. 11

D. 10

E. 0

2  What is the output of the following fragment?

     for (int i = 0; i < 15; i++) {

       if (i % 4 == 1)

         System.out.print(i + " ");

     }

A. 1 3 5 7 9 11 13 15

B. 1 3 5 7 9 11 13

C. 1 5 9 13

D. 1 4 8 12

E. 1 5 9 13 16

3  A continue statement can be used only in a loop.

A. true

B. false

4  A break statement can be used only in a loop.

A. true

B. false

5  The elements inside the for loop control are separated using semicolons instead of commas.

A. false

B. true

6  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 (0 < x) && (x < 100) is not enclosed in a pair of parentheses.

C. The numbers 1 to 99 are displayed.

D. The code does not compile because the loop body is not in the braces.

E. The numbers 2 to 100 are displayed.

7  What balance after the following code is executed?

int balance = 10;

while (balance >= 1) {

   if (balance < 9) continue;

   balance = balance - 9;

}

A. 0

B. 2

C. The loop does not end

D. 1

E. -1

8  What is y after the following for loop statement is executed?

int y = 0;

for (int i = 0; i < 10; ++i) {

    y += 1;

}

A. 9

B. 12

C. 10

D. 11

9  Assume x is 0. What is the output of the following statement?

if (x > 0)

    printf("x is greater than 0");

else if (x < 0)

    printf("x is less than 0");

else

    printf("x equals 0");

A. x is less than 0

B. x equals 0

C. x is greater than 0

D. None

10  What the output of the following code:

for ( ; ; )

   System.out.println("Welcome to Java");

A. prints out Welcome to Java one time.

B. prints out Welcome to Java two times.

C. does not print anything.

D. prints out Welcome to Java forever.

Part II. (3 pts)

Suppose the input is 2 3 4 5 0. What is the output of the following code?

import java.util.Scanner;

public class Test {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int number, max;

number = input.nextInt();

max = number;

do {

number = input.nextInt();

if (number > max)

max = number;

} while (number != 0);

System.out.println("max is " + max);

System.out.println("number " + number);

}

}

Part III. Show the printout of the following code:

(a) (2 pts each)

public class Test {

/** Main method */

public static void main(String[] args) {

for (int i = 1; i < 5; i++) {

int j = 0;

while (j < i) {

System.out.print(j + " ");

j++;

}

}

}

}

(b) (4 pts each)

public class Test {

/** Main method */

public static void main(String[] args) {

int i = 0;

while (i < 5) {

for (int j = i; j > 1; j--)

System.out.print(j + " ");

System.out.println("****");

i++;

}

}

}

Part V: (5 pts each):

1. Write a complete program that prints numbers from 1 to 100, every ten numbers per line.

2. Write a loop that displays the following pattern.

12345678987654321

234567898765432

3456789876543

45678987654

567898765

6789876

78987

898

9

Key for Exam2

Part I: Multiple Choice Questions.

Keys:

1. D

2. C

3. A

4. B

5. B

6. B

7. C

8. C

9. B

10. D

Part II.

max is 5

number 0

Part III. Show the printout of the following code:

(a)

0 0 1 0 1 2 0 1 2 3

(b)

****

****

2 ****

3 2 ****

4 3 2 ****

Part IV:

(a)

public class Test {

/**Main method*/

public static void main(String[] args) {

for (int i = 1; i ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download