You are on page 1of 28

1. A programmer claims that, in the following code fragment, task A will never be performed.

Which of the following supports the programmer's claim? double x = 19.9999; if (x >= 20.0 && x <= 0.0) perform task A else perform task B

(a) Any value of x would make the condition true.

(b) The condition could never be true for any value of x.

(c) The value of x is close enough to 20.0 that they could be considered equal.

(d) Only one branch of an if-else-statement can be executed. Correct answer is (b) 7. Consider the following Java program segment. for (int i=0; i<3; i++) { for (int j=0; j<2; j++) { if ( i == j ) { continue; } System.out.print("i="+ i + ", j=" + j + "; "); } } Which of the following is the complete output when the program segment is executed?

(a) i=0, j=1; i=1, j=0; (b) i=1, j=1; i=2, j=1; (c) i=0, j=1; i=1, j=0; i=2, j=0; i=2, j=1; (d) i=0, j=0; i=1, j=1; i=2, j=2; Correct answer is (c)

5. The following code fragment contains how many Java keywords? public class Hello extends Applet { . . . }

(a) Two (b) Three (c) Four (d) Five Correct answer is (b)

3. Which of the following are dictated by a variable's type? the amount of space in memory the variable will occupy the operations in which it can be used the naming convention for the variable's identifier (a) I, II, and III (b) II only (c) I and II only (d) I and III

Correct answer is (c)

4. Which of the following are primitive Java types? int Integer Double (a) I and II only (b) I only (c) I, II and III only (d) II and III only Correct answer is (b)

6. If a program that is written for one type of machine can run, without major modifications, on another type of machine as well, then the program is said to be (a) portable (b) importable (c) extendable (d) exportable Correct answer is (a)

8. int sum = 0; int count = 18; int i = 1; for (i = 0; i < count; i = i + 2) sum = sum + i; What does the above loop do?

(a) It computes the sum of the even numbers from 0 through count - 1 (b) It computes the sum of the odd numbers from 0 through count - 1 (c) It computes the sum of the odd numbers from 0 through count (d) It computes the sum of the even numbers from 0 through count Correct answer is (a)

7. Consider a Java for-loop with the following form. for (initialization; loopGuard; update) loopBody Among the three placeholders, initialization, loopGuard, and update, which are optional in a legal Java for-loop?

(a) initialization (b) None (c) update (d) All Correct answer is (d) 9. What is the value of product when the following loop completes? int i = 3; int product = 1; while (i != 0) { product = product * i; i = i - 1; }

(a) 3 (b) 1 (c) 0 (d) 6 Correct answer is (d)

9. int i = 1, sum = 0; while (i < 3){ int j = 0; while ( j < 2){ sum = sum + j; j = j + 1; } i = i + 1; }

What is the value of sum after the loop? (a) 0 (b) 2 (c) 6 (d) 4 Correct answer is (b)

10.

What is the value of product when the following loop completes? int i = 3; int product = 1; while (i != 0) { product = product * i; i = i - 1; }

(a) 1 (b) 3 (c) 6 (d) 0 Correct answer is (c)

15. In Java, the scope of a parameter of a method contains the scope of which of the following?

(a) Local variables of the method (b) Global variables of the method (c) Instance variables of the method (d) Class variables of the method Correct answer is (a)

16. In the class defined below class MyClass {

int age = 19; void myMethod() { int counter; } }

(a) both age and counter are instance variables

(b) age is a local variable and counter is an instance variable

(c) age is an instance variable and counter is a local variable

(d) both age and counter are local variables

Correct answer is (c)

18. Consider the following Java program segment. String a = "A"; String b = "B"; String c = "C"; String d = "D"; Vector v = new Vector(); v.add(a);

v.add(b); v.add(c); v.add(d); At the end of execution of the program segment, what value will be returned by v.elementAt(1)?

(a) "B" (b) "C" (c) "A" (d) "D" Correct answer is (a)

14. In Java, if x is a Boolean variable with the value true, which of the following expressions will evaluate to true? !x || x ! (x && !x) x == !x

(a) I only (b) I, II, and III (c) III only (d) I and II only Correct answer is (d)

16. The amount of memory used to store a variable depends on (a) the scope of the variable

(b) the type of the variable (c) the number of times the variable is accessed (d) whether the variable is declared in a class that extends class Applet Correct answer is (b)

10. Assume you are given a method with a specification double numbers(); that supplies you with numbers from the user's input. You need to find the first input number from user input that is smaller than 0.5 and put it in the variable x. Which of the following can be used for that purpose? double x = numbers(); while (x <= 0.5) x = numbers(); double x = 0.1; while (x >= 0.5) x = numbers(); double x; for (x = numbers(); x >= 0.5; x = numbers());

(a) III only (b) II and III only (c) I only (d) I, II and III Correct answer is (a)

8. int i, j; for (i = 0; i < 3; i = i + 1) for (j = 0; j < i; j = j + 1){ System.out.println(" " + j); }

How many lines of output will be produced? (a) 0 (b) 6 (c) 3 (d) 9 Correct answer is (c)

9. What is the value of product when the following loop completes? int i = 3; int product = 1; while (i != 0) { product = product * i; i = i - 1; }

(a) 1 (b) 6 (c) 0 (d) 3 Correct answer is (b)

3. Which of the following are valid Java statements? i = i; // y = 5;

x = 10

(a) I and II only (b) II and III only (c) I only (d) III only Correct answer is (c)

4. Which of the following data types is a Java primitive type?

(a) void (b) applet (c) boolean (d) class Correct answer is (c)

6. Which of the following is (are) true of any Java bytecode file? It can be executed by a Java Virtual Machine. It is in human-readable form. It can be executed on more than one computer platform.

(a) I and II only (b) II and III only

(c) I only (d) I and III only Correct answer is (d)

1. Assume that max, x, and y are Java variables of type int and assume that x and y have different values. Execution of which of the following Java program segments will result in the larger of x and y being assigned to max? if (x > y) max = x; else max = y; if (y - x <= 0) max = x; else max = y; max = x; if (x < y) max = y;

(a) I only (b) I and II only (c) I, II, and III (d) III only Correct answer is (c)

2. In Java, non-sequential execution of code is achieved by using _____ statements.

(a) concurrent (b) ordering (c) branching (d) control Correct answer is (d)

2. Consider the following program segment. public String nameOfEmployee (int salary) { if (salary < 50000) { if (salary > 45000) return "Joe"; } else if (salary < 60000) return "John"; else return "Jack";

return null; } If the method nameOfEmployee is called with the value 55000 as its argument, what String value will be returned?

(a) "John" (b) "Joe" (c) null (d) "Jack" Correct answer is (a) 4. The type of the number 2.0 in Java can be

(a) int (b) Panel (c) boolean (d) double Correct answer is (d)

4. In Java, the range of values of a variable is determined by the

(a) Java interpreter (b) scope of the variable (c) Java compiler (d) data type of the variable Correct answer is (d)

5. Which of the following statements best describes objects in Java?

(a) An object is everything included into the program with the import command. (b) An object is a data-structure equipped with methods for manipulating that data. (c) An object is a set of classes that share a common structure and a common behavior. (d) An object is a collection of methods that produce graphical elements in the applet or application window, or compute and return values. Correct answer is (b)

8. What is the output of the following loop? Assume the method print prints its argument on the screen. int i, j; for (i = 1; i <= 3; i = i + 1) for (j = 0; j < 3; j = j + 1) System.out.print(i * j + " ");

(a) 0 1 1 0 2 4 0 3 9 (b) 0 0 0 1 2 3 2 4 9 (c) 0 1 2 0 2 4 0 3 6 (d) 1 2 3 2 4 6 3 6 9 Correct answer is (c)

9. What does the following loop compute, assuming n is a positive even integer? int i = 1; int sum = 0; while (i <= n) { if (i % 2 == 0) sum = sum + i; i = i + 1; }

(a) Computes the sum of the odd integers from 1 through n.

(b) Computes the sum of the even integers from 1 through n - 1.

(c) Computes the sum of the even integers from 1 through n.

(d) Computes the sum of the odd integers from 1 through n - 1.

Correct answer is (c)

12. In Java, the operator "+" can be used for which of the following purposes? Addition of two integers Concatenation of two strings Addition of two floating point variables

(a) I and III only (b) II and III only (c) I only (d) I, II, and III Correct answer is (d)

14. Consider the following Boolean expression. !(!(a&&b)) For which of the following values of a and b, respectively, will the expression

evaluate to true?

(a) false, true (b) false, false (c) true, true (d) true, false Correct answer is (c)

15. The scope of an identifier is the

(a) area of a program where that identifier can be used (b) list of methods that can use the identifier (c) number of times in the program the identifier was used (d) list of classes that can use the identifier Correct answer is (a)

17. Reusable collection classes are included in which of the following Java packages?

(a) java.util (b) java.net (c) java.text (d) java.io Correct answer is (a)

18. Which of the following Java statements correctly declares and instantiates a Vector v?

(a) Vector v = new Vector(v); (b) v = new Vector(); (c) Vector v = Vector(); (d) Vector v = new Vector(); Correct answer is (d)

2. Consider the following program segment. public String assignString(int x) { String s=null; if (x<5) s = "less than 5"; if (x<10) s = "less than 10"; else s = "greater than 10"; return s; } If the method assignString is called with the value 4 as its argument, what String value will be returned?

(a) null (b) "less than 10" (c) "less than 5" (d) "greater than 10" Correct answer is (b)

4. Which of the following are dictated by a variable's type? the amount of space in memory the variable will occupy the operations in which it can be used the naming convention for the variable's identifier

(a) I and III (b) II only (c) I and II only (d) I, II, and III Correct answer is (c)

5. The purpose of the interpreter called java in the JDK is to

(a) produce Java .class files (b) execute Java .class files (c) compile Java source code (d) edit Java source code Correct answer is (b)

6. The term Java Virtual Machine refers to

(a) a compiler of Java source code (b) a household appliance operated by Java (usually a smart toaster) (c) an interpreter of Java bytecode (d) a hardware extension needed to run Java Correct answer is (c)

7. Assume that variable n has been declared to be of type int, assume that n has a positive value, and consider the following Java program segment. int s = 0; for (int i = n; i > 0; i = i - 1) s = s + i; The value of s at the end of any execution of the program segment is the sum of the

(a) integers from 1 through n (b) integers from 1 through n 1 (c) even integers from 1 through n (d) odd integers from 1 through n Correct answer is (a)

8. Consider the following outline of a code segment that includes a for-loop with a blank.

int total = 0; for (int i = 10 ; _____ ; i--) total = total + i; System.out.print(total); Which of the following expressions should be used to fill in the blank so that the value 49 will be output when the resulting code segment is executed?

(a) i == 3 (b) i > 3 (c) i >= 3 (d) i < 3 Correct answer is (b)

10. How many times will the following loop execute? boolean flag = false; int count = 0; while (flag = true) { if (count <= 10) count = count + 1; else flag = false; }

(a) infinitely many (b) 9 (c) 0

(d) 10 Correct answer is (a)

12. The following code segment is intended to sum together all of the even numbers in the range 1 through 40 and to store the final result in the variable sum. int sum = 0; for (int j = 1; j < 40; j+=2) sum = sum + j; Which of the following is an error (are errors) in the code segment that will prevent the intended result? The loop counter is not initialized correctly. The loop increments by the wrong amount. The loop guard causes the loop to terminate at the wrong place.

(a) I only (b) None (c) I and III (d) II and III Correct answer is (c)

13. Consider the following code fragment. int m = 3; int n = m / 2; What is the value of n?

(a) 1.0 (b) 1 (c) 1.5 (d) 2 Correct answer is (b)

14. Assume that isRaining, haveUmbrella, haveCoat, and shortTrip have been declared as type boolean and consider the following Java program segment. isRaining = true; haveUmbrella = false; boolean takeBus = isRaining && !((haveUmbrella && haveCoat) || shortTrip); At the end of execution of the program segment, the value of variable takeBus is

(a) dependent on the value of haveCoat (b) false (c) dependent on the value of shortTrip (d) true Correct answer is (c)

15. The region of a program where a variable can be referenced is known as the _____ of the variable.

(a) storage space (b) scope (c) type (d) conventional space Correct answer is (b)

16. What is the value of x after execution of the following Java code fragment? int x = 4; x = 3;

(a) Undefined (b) 4 (c) 3 (d) 2 Correct answer is (c) 17. Which of the following methods can be used to determine the number of elements contained in an instance of class java.util.Vector? (a) size (b) add (c) contains (d) capacity Correct answer is (a)

18. Which of the following is the term used for the action of informing the system of the true nature of an object reference in Java? (a) Instantiating

(b) Initializing (c) Declaring (d) Casting Correct answer is (d) 10. Which of the following Java constructs loop infinitely? while (true) i = 0; while (false) i = 1; while (!false) i = 0;

(a) III only (b) I, II and III (c) I only (d) I and III only Correct answer is (d)

12. What will be output upon execution of the following Java statement? System.out.println((int)(3.6%1.5*10));

(a) 0 (b) 10 (c) 9 (d) 6 Correct answer is (d)

13.

What is output when the following Java statement is executed? System.out.println(-5%-2);

(a) 0 (b) 2 (c) 1 (d) 1 Correct answer is (c)

14. If T represents the value true and F represents the value false, what does the following expression evaluate to?

! ( ( T || F ) && !F )

(a) There is an error in the expression so the program containing this expression cannot be compiled. (b) The expression cannot be evaluated based on the information presented. (c) true (d) false Correct answer is (d)

15. Consider the Java code below. class MyClass {

int age; void myMethod() { int counter = 19; age = counter; } } Which of the following is true concerning the code?

(a) myMethod will generate a compile-time error because age cannot be referenced. (b) age and counter will always contain the same value. (c) age is an instance variable and counter is a local variable. (d) age and counter have the same scope. Correct answer is (c)

16. In Java, which of the following identifiers are correct? MYCLASS my_Class myclass.1

(a) I, II, and III (b) I and II only (c) I and III only (d) II and III only Correct answer is (b)

17. Which of the following is a method invocation that retrieves the first element from an instance of class java.util.Vector?

(a) read(0) (b) elementAt(1) (c) elementAt(0) (d) read(1) Correct answer is (c)

18. In Java, which of the following strategies should be used to ensure that objects can be successfully retrieved from a Vector? Only references to objects of one class should be added to a Vector. Cast objects returned by method elementAt to the class of the object stored in the Vector. When processing elements in a Vector, always traverse the entire Vector.

(a) II only (b) I, II, and III (c) I and II only (d) II and III only Correct answer is (c)

You might also like