Exam 2 Study Guide

Report 6 Downloads 1505 Views
Exam 2 Study Guide Conditionals 1. What is the output of the following code? if (someNumber == 10) { System.out.println("A"); } else if (someNumber > 15 && someNumber 200) { System.out.println("C"); } else if (someNumber >= 1000) { System.out.println("D"); } else { System.out.println("E"); } if (someNumber < 0){ System.out.println("Tar"); } else if (someNumber < -10) { System.out.println("!"); }

(a) What is the output when someNumber is 0? (b) What is the output when someNumber is 10? (c) What is the output when someNumber is -100? (d) What is the output when someNumber is 1000? (e) Is there any unreachable code? If yes, what is(are) the String value(s) that we can never print out? Answer: (a) E; (b) A; (c) E Tar; (d) C; (e) Yes, “D”, “!”.

2. Given the following code: if (num > 30) { if (num == -1) { System.out.print("java" + " "); } else { System.out.print("CS major"+ " "); } if (num > 100) { System.out.println("rocks!"); } } else { System.out.println("Cool!"); } (a) What is the output when num is 101? (b) What is the output when num is -1?

Answer: (a) CS major rocks! (b) Cool! 3. The body code of an else statement will only run when all the test conditions of the prior if/else-if statement(s) are evaluated to be false.

Answer: True. 4. Within one if body of an if statement, there can be another set of if-else statement.

Answer: True.

5. Write the syntax of if-else statement. if () { < true body > } else { < false body> }

6. Using if-else statement, write a program that prints “Upper Half” when number is greater than or equals to 50 and “Lower Half” if it is lower than 50. if (n >= 50) { System.out.println(“Upper Half”); } else { System.out.println(“Lower Half”); }

7. Using if-else statement, write a program that prints “Correct” if the String variable s equals “red”, if not prints “Incorrect”. if (s.equals(“red”)) { System.out.println(“Correct”); } else { System.out.println(“Incorrect”); }

8. After the code runs, what will be printed? String fruit = "apple"; if(fruit.equals("orange")) { System.out.println("orange"); } else if(fruit.equals("banana")) { System.out.println("yellow"); } else { if(fruit.equals("pear")) { System.out.println("green"); } else if(fruit.equals("Gala apple".substring(5))) { System.out.println("red"); } } a) orange b) yellow c) green d) red D) red

9. After the following code runs, what will be printed? if (5 + 9 == 14 && 7 > 9) { System.out.print("GO"); } else if(9 + 10 == 21) { System.out.println("TARHEELS!"); } else if(7 - 5 == 3 || 9 - 5 > 3) { System.out.println("HEELS!"); } else { System.out.println("WOO!"); } a) WOO! b) GO HEELS! c) HEELS! d) TARHEELS! C) HEELS!

Loops 10.

Write the syntax of both while loop and for loop. while ( ) { <while body> } for (; ; ) { }

11. True/false: The three parts within the pair of parentheses of a for loop are counter declaration, initialization, and modification. False. The three parts are declaration and initialization (as a whole), bound, and modification.

12.

True/false: A for loop can always be rewritten as a while loop. True

13.

Using a while loop, write a loop that calculates the factorial of 10 (10!). int i=1, num=1; while (i ) { <method body> }

19. Write a public method named “combineWords” that has String parameters s1 and s2, then returns a String variable that concatenates those two variables. public String combineWords(String s1, String s2){ return s1+s2; }

20. Write a private method named “addThreeNumbers” that has one boolean parameter and three double parameters. It should return the sum of three variables if the boolean variable is true, otherwise returns “0” if false. The method’s return type should be double. double addThreeNumbers(boolean b, double x, double y, double z) { if (b) { return x + y + z; } else { return 0; } }

21.

Given the following method compare(int, int) below: boolean compare(int a, int b) { if (a > b) { return true; } else { return false; } }

A. What is the return type of this method? B. What boolean value would be returned if in the main method we call the compare method with arguments (4, 3)? C. What about with arguments (3, 3)?

A. boolean - B. true - C. false

22.

Given the following methods: public int a(int x) { if (x < 5) { return this.b(x) + this.b(2); } else if (x == 4) { return this.b(x) + 2; } else if (x >= 5) { return this.b(x + 2); } else { return x + 2; } } public int b(int x) { int y = 0; for (int i = 0; i < x; i++) { y++; } return y; }

What is returned by the following method calls? A. this.a(5) 7 B. this.a(4) 6 C. this.a(7) 9

Using Classes 23. Given the following class: public class CookieMonster { private int _cookies; public CookieMonster(int cookies) { _cookies = cookies - 2; } public String yell() { String cookie = "COOKIES!!!!!"; String a = cookie.substring(3); String b = ""; String c = cookie.substring(0, 1); for (int i = 0; i < _cookies; i++) { b += "O"; } return c + b + a;

} } What is printed when the following code runs: CookieMonster c = new CookieMonster(5); System.out.println(c.yell()); The output is: COOOKIES!!!!!

24. Given the following class: public class Snack { private String _name; private double _calories; private int _unitEaten; public Snack(String name, double calories) { _name = name; _calories = calories; _unitEaten = 0; } public void eat() { _unitEaten++; if (_unitEaten = 4) { System.out.println("Found your favorite!"); } else { System.out.println(“Fair enough”); } } public String aboutThisSnack() { return "This is " + _name + ", and it has " + _calories + "calories."; } }

What would be the output of the following code: Snack brownie = new Snack("brownie", 208); for (int i = 0; i
Recommend Documents