Professional Documents
Culture Documents
QUIZ I
COMP285 OBJECT-ORIENTED PROGRAMMING IN JAVA
10. Break statement in a nested loop results in ending execution of innermost loop. 11. In Java instance variables could be accessed/altered by all the methods of the class, but local variables could be accessed/altered only inside the method and the block in which they are declared.
Page 2 of 5
Wentworth Institute of Technology 2. List 2 advantages of Java over C++. 1) portability: the same program for every platform 2) object-oriented programming enforced 3) garbage collector collecting unreferenced objects 4) no memory management using pointers
3. The switch statement makes a decision based on Controlling_Expression, which must be of one of the following types: byte, short, int, long, char, enum, String. 4. Underline the names which may be used as a variable names (identifiers) in Java: rate1, 1stPlayer, time_limit, new-object, new, numberOfWindows, num-students, final, long, short, class, myclass? 5. List all the primitive types in Java. byte, short, int, long, float, double, char, boolean 6. Declare 2 variables as strings and check if they have the same contents. String s1, s2; s1=hello; s2=hi; if (s1.equals(s2)) System.out.println(both strings have the same contents) 7. What are the 4 different loop statements in Java? while, do while, for, for each.
8. Declare and create a new object my_species of the class Species. Species my_species =new Species(); 9. Why do we declare instance variables private instead of public? To prohibit modification of instance variables of the class from other classes, since they can be modified incorrectly or inconsistently. 10. Is the following code valid? If not, why and how should it be corrected?
double dinnerBill = 26.99; int numberOfDollars = (int)dinnerBill; double tax = 15;
Page 3 of 5
11. What is the value of all the variables after the code is executed? int n=3, m=4; int result0 = n++ * m ; 12 int result1 = n * ++m; 20 int result2 = n+m/3; 5 int result3 = n+m%3; 6 double result4 = m/3.0; 1.666 int result5 = m/3.0; 1 long result6 = math.round(result4); 2 12. What is the output of the following code?
int numberOfBabies = 3; switch (numberOfBabies) { case 1: System.out.println("Congratulations!"); break; case 2: System.out.println("Wow. Twins!"); break; case 3: System.out.println("Wow. Triplets!"); case 4: case 5: case 6: System.out.print("Unbelievable: "); System.out.println(numberOfBabies + " babies!"); break; default: System.out.println("I don't believe you."); break;
} Output: Wow. Triplets! Unbelievable: 3 babies!
Page 4 of 5
Wentworth Institute of Technology 13. What is wrong with each of the 2 code samples below? do { //some code int answer = JOptionPane.showConfirmDialog(null, "End program?", "Click Yes or No:", JOptionPane.YES_NO_OPTION); }while(answer == JOptionPane.NO_OPTION);
Variable answer is local, its scope is the block {}, so it cannot be accessed outside of {} in the last line.
if (answer = JOptionPane.YES_OPTION) JOptionPane.showMessageDialog(null, "End"); else if (answer = JOptionPane.NO_OPTION) JOptionPane.showMessageDialog(null, "One more time");
Use == instead of = in 1st and 3rd lines. The way this code is written answer will be assigned
JOptionPane.YES_OPTION.
Page 5 of 5