Name:______
Covers Chapters 3 and4 / CSCI 1301 Introduction to Programming
ArmstrongAtlanticStateUniversity
Instructor: Y. Daniel Liang

Part I: Multiple Choice Questions.

  1. Analyze the following code:

// Enter an integer

String numString = JOptionPane.showInputDialog(null,

"Enter a number:",

"Exam Input", JOptionPane.QUESTION_MESSAGE);

int number = Integer.parseInt(numString);

if (number <= 0)

System.out.println(number);

a.The if statement is wrong, because it does not have the else clause;

  1. System.out.println(number); must be placed inside braces;
  2. If number is zero, number is displayed;
  3. If number is positive, number is displayed.
  4. number entered from the input cannot be negative.
  1. What is the output of the following code: (Please indent the statement correctly first. This will help you read the program.)

int x = 9;

int y = 8;

int z = 7;

if (x > 9)

if (y > 8)

System.out.println("x > 9 and y > 8");

else if (z >= 7)

System.out.println("x <= 9 and z >= 7");

else

System.out.println("x <= 9 and z < 7");

  1. x > 9 and y > 8;
  2. x <= 9 and z >= 7;
  3. x <= 9 and z < 7;
  4. None of the above.
  1. Analyze the following code:

String numString = JOptionPane.showInputDialog(null,

"Enter a number:",

"Exam Input", JOptionPane.QUESTION_MESSAGE);

int number = Integer.parseInt(numString);

if (number <= 0)

System.out.println(number);

System.out.println(number);

  1. number is always printed out at least once;
  2. number is printed out twice if number is zero;
  3. number is printed out twice if number is negative;
  4. number is printed out once if number is positive.
  5. All of the above.
  1. What is y after the following switch statement?

int x = 0;

int y = 0;

switch (x + 1) {

case 0: y = 0;

case 1: y = 1;

default: y = -1

}

a.1

b.-1

c.0

d.None of the above

  1. Analyze the following code.

int x = 0;

int y = ((x<100) & (x>0)) ? 1: -1;

a.The code has a syntax error because & must be &.

b.y becomes 1 after the code is executed.

c.y becomes -1 after the code is executed.

d.None of the above.

  1. Which of the following is not a valid boolean expression.

a.(1 < x < 100)

  1. (x = 1) || (x != 1)
  2. (x =< 5) & (x>=5)
  3. a, b, and c are all correct
  4. a, b, and c are all wrong
  1. Which of the following expression is equivalent to (x > 1).

a.x >= 1

  1. !(x <= 1)
  2. !(x = 1)
  3. !(x < 1)
  4. None of the above
  1. Analyze the following two code fragments.

(i)

int x = 5;

if (0 < x) & (x < 100)

System.out.println("x is between 1 and 100");

(ii)

int x = 5;

if (0 < x & x < 100)

System.out.println("x is between 1 and 100");

a.The first fragment has a syntax error on the second line.

b.The second fragment has a syntax error on the second line.

  1. Both fragments produce the same output.
  2. Both fragments compile, but produce different result.
  1. None of the above.
  1. Analyze the following fragment.

double x = 0;

double d = 1;

switch (d + 4) {

case 5: x++;

case 6: --x;

}

a.The required break keyword is missing in the switch statement.

b.The required default keyword is missing in the switch statement

  1. The switch control variable cannot be double
  2. a, b, and c are all correct.
  3. a, b, and c are all incorrect.
  1. Analyze the following code.

int x = 0;

if (x > 0);

{

System.out.println("x");

}

a.The symbol x is always printed.

b.The value of variable x is always printed.

c.Nothing is printed because x > 0 is false.

d.None of the above.

11.Which of the loop statements always have their body executed at least once.

a.The while loop

b.The do-while loop

c.The for loop

d.None of the above

12.Analyze the following code.

int x = 1;

while (0 < x) & (x < 100)

System.out.println(x++);

  1. The loop runs for ever.
  2. The code does not compile because the loop body is not in the braces.
  3. The code does not compile because (0 < x) & (x < 100) is not enclosed in a pair of parentheses.
  4. The number 1 to 99 are displayed.
  5. The number 2 to 100 are displayed.

13.Analyze the following code.

double sum = 0;

for (double d = 0; d < 10; sum += sum + d) {

d += 0.1;

}

a.The program has a syntax error because the adjustment statement is incorrect in the for loop.

b.The program has a syntax error because the control variable in the for loop cannot be of the double type.

  1. The program compiles but does not stop because d would always be less than 10.
  2. The program compiles and runs fine.

14.What is y after the following for loop statement is executed?

int y = 0;

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

y += 1;

}

  1. 9
  2. 10

c.11

d.12

e.None of the above.

15.What balance after the following code is executed?

int balance = 10;

while (balance >= 1) {

if (balance < 9) continue;

balance = balance - 9;

}

  1. -1
  2. 0
  1. 1
  2. 2
  3. The loop does not end

16.What is the value of balance after the following code is executed?

int balance = 10;

while (balance >= 1) {

if (balance < 9) break;

balance = balance - 9;

}

  1. –1
  2. 0
  3. 1
  4. 2
  5. None of the above

17.What is x after evaluating

x = (2 > 3) ? 2 : 3;

  1. 2
  2. 3
  3. 4
  4. None of the above

18. Analyze the following code:
boolean even = ((231 % 2) == 0);
if (even = true)
System.out.println("It is even!");

else
System.out.println("It is odd!");

a. The program has a syntax error

b. The program has a runtime error

c. The program displays “It is odd!”

d. The program displays “It is even!”

e. None of the above

19. Analyze the following code:

a. A and B are equivalent.

b. A and C are equivalent.

c. B and C are equivalent.

d. A, B and C are equivalent.

Part II. (1 pts) Correctly indent the following statement.

if (i > 0) if

(j > 0)

x = 0; else

if (k > 0) y = 0;

else z = 0;

Part III. (5 pts) Identify and fix errors in the following code:

public class Test

{

public static void main(string[] args)

{

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

{

if (j > i) then

j++

else

j--;

}

}

}

Part IV. (5 pts) Show the printout of the following code:

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 V: (5 pts each):

1. Write a program that prints numbers from 1 to 50, but for numbers that are multiples of 5, print HiFive, and for numbers that are divisible by both 2 and 3, print Georgia.

2. Write a loop that computes


3. 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.

  1. c
  2. d
  3. e
  4. b
  5. c
  6. e
  7. b
  8. a
  9. c
  10. a
  11. b
  12. c
  13. d
  14. b
  15. e
  16. c
  17. b
  18. d
  19. b

Part II. (1 pts) Correctly indent the following statement.

if (i > 0)

if (j > 0)

x = 0;

else if (k > 0)

y = 0;

else z = 0;

Part III. (5 pts) Identify and fix errors in the following code:

public class Test

{

public static void main(string[] args) // should be String

{

for (int i = 0; i < 10; i++); // should remove ;

{

if (j > i) then // should remove then // j not defined

j++ // miss ;

else

j--;

}

}

}

Part IV. (5 pts) Show the printout of the following code:

1bb

1bb3bb

1bb3bb9bb

1bb3bb9bb27bb

Part V:

(a)

import javax.swing.JOptionPane;

public class Test {

/**Main method*/

public static void main(String[] args) {

// Prompt the user to enter sales amount

String doubleString = JOptionPane.showInputDialog(

"Enter sales amount:");

// Convert string to double

double salesAmount = Integer.parseInt(doubleString);

// Compute sales commission

double salesCommission = 0;

if (salesAmount <= 5000)

salesCommission = salesAmount * 0.08;

else if (salesAmount <= 10000)

salesCommission = 5000 * 0.08 + (salesAmount - 5000) * 0.1;

else

salesCommission = 5000 * 0.08 + 10000 * 0.1 +

(salesAmount - 10000) * 0.12;

// Display results

System.out.println("Sales commission is " + salesCommission +

" for sales amount of " + salesAmount);

}

}

(b)

import javax.swing.JOptionPane;

public class Test {

/**Main method*/

public static void main(String[] args) {

double sum = 0;

for (int i = 1; i <= 99; i++)

sum += sum + (i * 1.0 / (i + 1));

// Display results

System.out.println("Sum is " + sum);

}

}

(c)

public class Test {

/**Main method*/

public static void main(String[] args) {

for (int row = 1; row <= 9; row++) {

// Print spaces

for (int column = 1; column < row; column++)

System.out.print(" ");

for (int column = row; column <= 9; column++)

System.out.print(column);

for (int column = 8; column >= row; column--)

System.out.print(column);

System.out.println();

}

}

}

1