Sample Quiz4 Answers
Sample Quiz4 Answers
Sample Quiz4 Answers
CS 151
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
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.
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?
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
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.
ANS: C 13. Overloading is
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.
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
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.
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.
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.
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
CS 151
Answers
1 B B 2 D 3 C 4 C 5 C 6 B 7 A 8 9 10 11 12 13 14 15 16 17 18 19 20 21 D B C C B A A D C D C D A
ECC
Niko ulevski