{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Practice Exam 1 (from Susan Horwitz)

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

Info icon This 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? 3. Which of the following best describes the circumstances under which the expression !(a && b) && (a || b) evaluates to true ?
Image of page 1

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

View Full Document Right 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? 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.
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?
Image of page 3

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

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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern