You are on page 1of 5

CS 151

1. What will be the value of x after the following code is executed? int x, y = 4, z = 6; x = (y++) * (++z); a. b. c. d. 24 28 30 35

ANS: B

2. a. b. c. d.

A(n) ___________ will always be executed at least once. pre-test loop post-test loop sentinel loop for loop

ANS: B 3. What will be the value of x after the following code is executed?

int x = 10, y = 20; while (y < 100) { x += y; } a. 90 b. 110 c. 210 d. This is an infinite loop
ANS: D 4. What will be the value of x after the following code is executed?

int x = 10; do { x *= 20; } while (x > 5); a. b. c. d. 10 200 This is an infinite loop. The loop will not be executed, the initial value of x > 5.

ANS: C
ECC

Niko ulevski

CS 151

## 5. How many times will the following for loop be executed?

for (int count = 10; count <= 21; count++) System.out.println("Java is great!!!"); a. b. c. d. 1 10 11 0

ANS: C 6. The _________ is ideal in situations where the exact number of iterations is known.

a. b. c. d.

## while loop do-while loop for loop if statement

ANS: C 7. Before entering a loop to compute a running total, the program should first

a. b. c. d.

read all the values into main memory. set the accumulator where the total will be kept to an initial value, usually zero. know exactly how many values there are to total. set all variables to zero.

ANS: B 8. What will be the value of x after the following code is executed?

## int x, y = 15, z = 3; x = (y--) / (++z); a. b. c. d. 3 4 5 6

ANS: A

9. What values for number could you enter to terminate the following while loop (assume Keyboard is a given class that reads input with appropriate methods for different typesin this case readInt() to read integers)? System.out.print("Enter a number: "); int number = Keyboard.readInt(); while (number < 100 || number > 500) { System.out.print("Enter another number: number = Keyboard.readInt(); } a. Numbers less than 100 b. Numbers greater than 500 c. Numbers in the range 100 - 499
ECC

");

Niko ulevski

CS 151

## d. Numbers in the range 100 - 500

ANS: D

10. Which of the following will not convert a string to a number? a. Double.parseDouble( str ) b. Int.parseInt( str ) c. Short.parseShort( str ) d. Byte.parseByte( str )
ANS: B 11. Which of the following will open a file and allow you to append data to its existing contents?

a. PrintWriter outfile = new PrintWriter(fwriter, true); b. PrintWriter outfile = new PrintWriter(fwriter); c. FileWriter fwriter = new FileWriter("MyFile.txt", true); d. FileWriter fwriter = new FileWriter("MyFile.txt");
ANS: C 12. Which of the following is not true about static methods?

a. It is not necessary for an instance of the class to be created to execute the method. b. They are created by placing the key word static after the access specifier in the method header. c. They are called from an instance of the class. d. They are often used to create utility classes that perform operations on data, but have no need to collect and store data.

a. b. c. d.

writing a method that does too much processing. having two or more methods with the same name, but different signatures. having two or more methods with the same signature. writing a program that is too large to fit in memory.

## ANS: B 14. If you write a constructor for a class, you should

a. also write a no-parameter constructor, if you want to create an instance of the class without passing arguments to the constructor. b. identify the default constructor. c. not be concerned, the Java compiler will write one for you. d. set all class data members to 1.
ANS: A 15. If the this variable is used as a constructor call

a. b. c. d.

a compiler error will result if it is not the first statement of the constructor. a compiler error will result if it is not the first statement of the constructor. a compiler error will result. the this variable cannot be used as a constructor call.

ANS: A
ECC

Niko ulevski

CS 151

## 16. The only limitation that static methods have is

a. b. c. d.

they can refer to only non-static members of the class. they can only be called from static members of the class. they must be declared as public methods. they cannot refer to non-static members of the class.

## ANS: D 17. A method's signature consists of

a. b. c. d.

the return type and the method name. the return type, the method name, and the parameter list. the method name and the parameter list. the method name.

## ANS: C 18. The default constructor that Java provides

a. sets all the class's numeric fields to 0, boolean values to true, and reference variables to null. b. sets all the class's numeric fields to 0, boolean values to false, and reference variables to spaces. c. sets all the class's numeric fields to 0, boolean values to true, and reference variables to spaces. d. sets all the class's numeric fields to 0, boolean values to false, and reference variables to null.
ANS: D 19. The compiler expects the package statement

a. b. c. d.

to be the first statement after the class header. to be the first statement in the main method. to be the first statement in the file. to be the last statement in the file.

## ANS: C 20. To compare two objects in a class,

a. use the ==, e.g. object1 == object2. b. write a method to do a byte-by-byte compare of the two objects. c. write an equals method that will make a field by field compare of the two objects. d. since objects consist of several fields, you cannot compare them.
ANS: D

21. When calling a static method you supply a. the name of the class containing the method. b. a reference to an object of the class containing the method. c. a variable name. d. you cannot call a static method Answer: A

ECC

Niko ulevski