You are on page 1of 5

Wentworth Institute of Technology

Computer Science and Systems Department

QUIZ I
COMP285 OBJECT-ORIENTED PROGRAMMING IN JAVA

Name: __________________________________________________________Score:____________out of: 55 TRUE OR FALSE (1 PT EACH)


1. F______Java is an extension of C++. 2. T______ All program variables must be declared before you use them. 3. T______ Identifiers in Java are case sensitive. 4. T______ You can run Java programs and applets on any computer, no matter what the OS, as long as it's equipped with the Java Virtual Machine. 5. F_______JVM is a Java compiler translating a Java program into a machine language for a particular computer. 6. F_______Java uses call-by-value parameter passing mechanism for all arguments of methods.

FILL IN THE BLANK (1 PT EACH BLANK)


1. Java was designed as a language for home appliances, and later to support web programming. 2. Applications are regular programs meant to be run on your computer, applets are little programs that can be sent to another location on the internet and run there. 3. The Java compiler translates a Java program into byte-code for a particular computer, which is then translated by JVM into machine language for a particular computer. 4. Class definition consists of declaration of instance variables and methods of the class. 5. The methods in the class Scanner can be used to read keyboard input. 6. What is the keyword declaring a constant? final 7. For a string variable greeting how we refer to its 1st character greeting.CharAt(0), last character greeting.CharAt(greeting.length()-1), substring consisting of 2nd, 3rd, and 4th characters of the string__ greeting.substring(0,4). 8. For keyboard input in Java we use class Scanner, for GUI windows input/output we use class JOptionPane. 9. An enumeration type is a class type listing all the values that a variable of this class can have. We declare it when we want to restrict the possible values of the variable to just few values. Dr. Olga Lepsky Page 1 of 5

Wentworth Institute of Technology

Computer Science and Systems Department

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.

MULTIPLE CHOICE (1 PT EACH)


1. What is Java? D A. an island in Indonesia B. a brewed coffee C. a platform independent computer programming language D. all of the above 2. When was Java invented? D A. 1960s B. 1970s C. 1980s D. 1990s E. 2000s 3. What happens when an expression uses == to compare two string variables? B A. The value of the expression will be true if both strings have the same characters in them. B. The value of the expression will be true if both string variables refer to the same object. C. The expression will not be correct syntax and the program will not compile. 4. How is Java program translated into machine language? C A. via compiler (a program that translates the entire program) B. via interpreter (a program that translates and executes blocks of code at a time)
C. via both

SHORT ANSWER QUESTIONS (2 PTS EACH)


1. What makes Java suitable for Internet applications? After compiling a Java program into bytecode, that bytecode can can be sent over the Internet to any computer with a bytecode interpreter (JVM).

Dr. Olga Lepsky

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

Computer Science and Systems Department

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;

Dr. Olga Lepsky

Page 3 of 5

Wentworth Institute of Technology

Computer Science and Systems Department

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!

Dr. Olga Lepsky

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);

Computer Science and Systems Department

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.

Dr. Olga Lepsky

Page 5 of 5

You might also like