Practice Exam 1 (from Susan Horwitz)

Practice Exam 1 (from Susan Horwitz) - CS302 Fall 2006:...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
1 CS302 Fall 2006: Practice Exam 1 1. Assume that x , y , and z are all int variables. Consider the following code segment: if (x == 0) { if (y == 1) z += 2; } else { z += 4; } System.out.print(z); What is printed if x , y , and z are all equal to zero before the code segment executes? A. 0 B. 1 C. 2 D. 4 E. 6 2. Assume that the following variable declarations have been made. int j, k; double d1, d2; boolean b; Which of the following statements would not compile? A. j += k * 2; B. d1 = (double)k/d2; C. j = k * b; D. b = !b; E. b = (j == k); 3. Which of the following best describes the circumstances under which the expression evaluates to true ? A. Always B. Never C. Whenever both a and b are true D. Whenever neither a nor b is true E. Whenever exactly one of a and b is true
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Questions 4 and 5 concern the following Animal class. public class Animal { private String name; private double age; // constructor public Animal( String n ) { name = n; age = 0.0; } public String getName() { return name; } public double getAge() { return age; } public void setAge(double a) { age = a; } } 4. Consider the following incomplete method. public void doSomething() { Animal s = new Animal("Spot"); <missing statements> } Assume that method doSomething is not part of the Animal class. Which of the following statements could be used to replace <missing statements> so that the code would compile? A. s.setAge(100); System.out.println(s.getAge()); B. s.age = 100; System.out.println(s.age); C. s.setAge(age + 1); System.out.println(s.getAge()); D. s.age = 95.5; System.out.println(s.age); E. s.setAge(95.5); System.out.println(s.setAge()); 5. Consider changing the Animal class so that both the name and the age of an animal can be set when an Animal object is created. For example: Animal an1 = new Animal("Spot", 10.0); // Spot’s age is 10.0 Animal an2 = new Animal("Rover" 25.5); // Rover’s age is 25.5 Which of the following best describes the change that should be made? A. Define a constructor with no arguments. B. Define a constructor with one argument. C. Define a constructor with two arguments. D. Define a method named setNameAndAge . E. It is not possible to change the Animal class as specified.
Background image of page 2
3 6. Consider the following code segment: int x = 0; boolean y = true; if (y & (x != 0) && (2/x == 0)) System.out.println("success"); else System.out.println("failure"); Which of the following statements about this code segment is true? A. There will be an error when the code is compiled because the first operator is applied to a non- boolean expression. B. There will be an error when the code is compiled because a boolean variable ( y ) and an int variable ( x ) appear in the same if -statement condition. C. There will be an error when the code is executed because of an attempt to divide by zero.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/27/2008 for the course CS 302 taught by Professor Willbenton during the Fall '07 term at Wisconsin.

Page1 / 13

Practice Exam 1 (from Susan Horwitz) - CS302 Fall 2006:...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online