intro to programming in java review for chapter 3 test. page 1 intro to programming in java review...

4
12-13 page 1 Intro to Programming in Java Review for Chapter 3 Test. Name Date Sections I and II are multiple choice and true/false. 1. What is the output produced by the following segment of code? int x = 10, y = 24; if ((x < y) && (y == 20)) System.out.print("One"); if ((x == y) || (x < y)) System.out.print("Two"); if ((x != y) || (x < y)) System.out.print("Three"); 2. What is the output produced by the following segment of code? int x = 10, y = 24; if (x != y) System.out.print("One"); else if (x < y) System.out.print("Two"); else System.out.print("Three"); 3. What type of quantities would be best represented with a boolean type variable? 4. What is the output produced by the following segment of code? for (int i = 0; i <= 5; i++) System.out.print("*");

Upload: buiduong

Post on 30-May-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

12-13 page 1 Intro to Programming in Java Review for Chapter 3 Test. Name Date Sections I and II are multiple choice and true/false. 1. What is the output produced by the following segment of code?

int x = 10, y = 24; if ((x < y) && (y == 20)) System.out.print("One"); if ((x == y) || (x < y)) System.out.print("Two"); if ((x != y) || (x < y)) System.out.print("Three");

2. What is the output produced by the following segment of code?

int x = 10, y = 24; if (x != y) System.out.print("One"); else if (x < y) System.out.print("Two"); else System.out.print("Three");

3. What type of quantities would be best represented with a boolean type variable? 4. What is the output produced by the following segment of code?

for (int i = 0; i <= 5; i++) System.out.print("*");

12-13 page 2 5. Write each of the following in Java shortcut format: a. y = y + x b) y = y – 4 c) y = y /2 d) y = y * 5 6. What is the major difference between a while loop and a do-while loop? 7. What is the output produced by the following segment of code?

int x = 80; int y = 24;

if (x >= 80) System.out.print("One"); if ((x <= 90) && (y >= 80)) System.out.print("Two"); if ((x <= 80) && (y <= 70)) System.out.print("Three");

8. What is the output produced by the following segment of code?

for(int i=1; i>=5; i--) System.out.println(i);

9. What is the output produced by the following segment of code?

for(int i=5; i>=1; i--) System.out.print(i); 10. What is the output produced by the following segment of code?

int x = 0, y = 1; while (y <= 6) { x += y; y += 2; } System.out.println( y + "-" + x);

12-13 page 3 11. What is the output produced by the following segment of code?

int n = 165,c = 0; while (n != 0) { n = n / 10; c++; } System.out.println(c + "*" + n);

12. In Java, how are the == and = symbols used? 13. What is the difference (if any) between if(a >= b) a++; else b--; and

if (a < b) b--; else a++; 14. When do you use an else statement in Java?

15. Which symbols are used to compare int, double, and char types? How do we compare String types? 16. When is the expression a||b true? Short Answer Questions: III. You will be asked to complete two short answer questions:

Topics you should be familiar with: • Writing for loops • Writing code for determining if a number is even or odd

12-13 page 4 IV. You will have 3 programs to write at the computer.

1. You will need to write a program that will analyze the number of times a tossed coin lands on

Heads or Tails. The output includes the number of heads and the percent heads as well as the number of tails and percent tails. You will need to use a for loop to count how many times you are to "toss" the coin. The user will be asked in the beginning how many times they want to toss the coin. This number will be your ending value in the for loop.

2. You will be asked to write a program that generates a series of random numbers between 0 and

9 until a zero is generated and then displays the length of the sequence. You will use a do while loop that includes a statement to print out each number until the number randomly generated is a zero. The loop will also keep track of how many times a number is generated. You might want to use length = 0 to start then within the loop have length increment by one (length ++) Program output should look similar to:

3 2 7 8 8 6 0 Length is 7

3. You will be asked to write a program that outputs all of the int values between 1 and 100 with

5 values per line, and each of those 5 values spaced out equally. You may want to review using \t so you output looks nice on the screen.

We will discuss writing these programs in class and you may bring your hand written notes to the test. You may not bring the entire program written out but the program written in pseudocode is fine.