Student Name__________________ Student Number_____________________
Multiple Choice ( /50)
1. The following shows all of the constructors for AuxClass which extends TheClass.
class AuxClass extends TheClass {
int nn;
AuxClass(){
nn=0;
}
AuxClass(int n){
nn=n;
}
//Aux Class continues
}
TheClass does not have any declared constructors. Which of the following are correct?
A. The compiler provides a default no-arguments constructor for
TheClass
B. The absence of a no-arguments constructor in TheClass causes a
compiler error
C. The compiler inserts a call to the default no-arguments constructor
in TheClass at the end of all AuxClass constructors.
D. The compiler inserts a call to the default no-arguments constructor
in TheClass at the start of all AuxClass constructors
2. You are required to create a class NewBase that extends TheBase and overrides a method in TheBase having the following signature.
protected void theMethod( float f ) throws IOException
Keeping in mind that IOException is a checked exception, which of the following statements must be true in NewBase is to compile correctly?
A. If the overriding method throws an exception, it must be the same or
less general than IOException.
B. The access modifier for the overriding method must be protected,
private or default.
C. The access modifier for the overriding method must be protected or
public.
D. If the overriding method throws an exception, it must be the same or
more general than IOException.
3. Which of the following are valid uses of the assert statement?
A. assert (i> 10) : "i is bigger than 10";
B. assert (i=10);
C. assert (i>10) : System.out.print("i is bigger than
10");
D. assert (i>10);
4. What will be printed by the following code?
int x=8;
System.out.println("Result" + (x>>2));
○A. Result 8
○B. Result 4
○C. Result 2
○D. Result 1
○E. Result 0
5. What will the output be when the following method is executed?
public static void main (String [] args) {
int n = 11;
do {
System.out.print(" " + n--);
}while((n % 4) != 0);
System.out.println("final" + n);
}
○A. 11 10 9 final 8
○B. 10 9 8 final 8
○C. 11 10 9 8 final 8
○D. 11 final 10
6. Consider the following variable declarations inside a method.
public void someMethod(){
int[] x;
int[] y = new int[20];
String[] s1;
String[] s2 = new String[20];
//method continues.
}
Select all of the following statements about these variables that are true
A. The value in x and s1 is null.
B. The value in y[20] is 0.
C. The value in s2[0] is an empty String.
D. The value in s2[0] is null.
7. Which of the following statements about interfaces is correct?
A. All methods in an interface have public accessibility
B. An interface may not contain any variables
C. Due to Java's principle of single inheritance, a class declaration
can only name one interface
D. All methods in an interface have default accessibility unless
declared otherwise.
8. What is the range of values which can be stored in a short primitive variable
○A. 0 to 255
○B. 0 to 32767
○C. -32768 to 32767
○D. 0 to \uFFFF
9. What will be the result of trying to compile and run an application
in which the following is the only declaration of a main method? (Assume the
rest of the class is correct.)
public
static void main(){
System.out.println(“hello world”);
}
○A. The class will compile without error but the program will not run
○B. The class will compile and run, writing “hello world” to the standard output.
○C. The compiler will report an error.
○D. The compiler will report an error but the program will run fine.
10. Java has how many ways of indicating comments:
○A. Two
○B. Three
○C. Four
11. You are writing a utility class and your company’s policy is to keep utility classes in a package named conglomo.util. Select the correct code fragment to start a class file for a class that uses classes in the java.awt and java.util standard library packages.
○A.
1. package java.conglomo.util
2. import java.awt.*
3. import java.util.*
○B.
1. package conglomo.util ;
2. import java.awt.* ;
3. import java.util.* ;
○C.
1. import java.awt.* ;
2. import java.util.* ;
3. import conglomo.util.* ;
○D.
1. package conglomo.util ;
2. import java.*.* ;
12. Consider the following application:
1.
class Q6 {
2.
public static void main(String args[]){
3. Holder h = new Holder();
4. h.held = 100;
5. h.bump(h);
6. System.out.println(h.held);
7.
}
8.
}
9.
10.
class Holder {
11.
public int held;
12.
public void bump(Holder theHolder) {
13. theHolder.held++;
14.
}
15.
}
What value is printed out at line 6?
○A. 0
○B. 1
○C. 100
○D. 101
13. Consider the following application:
1.
class Q7 {
2.
public static void main (String args[]){
3. double d=12.3;
4. Decrementer dec = new Decrementer();
5. dec.decrement(d);
6. System.out.println(d);
7.
}
8. }
9.
10.
class Decrementer {
11.
public void decrement (double decMe) {
12. decMe = decMe – 1.0;
13.
}
14.
}
What value is printed out at line 6?
○A. 0.0
○B. 1.0
○C. 12.3
○D. 11.3
14. What is the result of the following code segment?
double
d= 5/2;
System.out.println("The value is:
" + d);
○A. The value is: 2.0
○B. The value is: 2.5
○C. The value is: 3.0
15. What is –50 >> 1?
○A. A negative number with very large magnitude
○B. A positive number with very large magnitude
○C. -100
○D. -25
○E. 100
○F. 25
16. What is the result of running the following method with an input of 67?
1. public int
MaskOff( int N ) {
2. return N ^ 3;
3.
}
○A. The method will return 3.
○B. The method will return 64.
○C. The method will return 67.
○D. The method will return 0.
17. Suppose salaries is an array containing floats. Which of the following are valid loop control statements for processing each element of salaries.
○A. for (float f: salaries)
○B. for (int i: salaries)
○C. for (float f::salaries)
○D. for (int i::salaries)
18. Look at the following class definition:
public class DerivedDemo extends Demo{
int M, N, L ;
public DerivedDemo ( int x, int y ) {
M=x; N=y;
}
public DerivedDemo( int x ) {
super ( x );
}
}
Which of the following constructor signatures must exist in the Demo class for DerivedDemo to compile correctly? [Check all correct answers.]
A. public Demo( int a, int b)
B. public Demo( int c)
C. public Demo()
D. There is no requirement for a constructor in Demo
19. Consider the following classes, declared in separate source files:
//File Base.java
public class Base {
public void method ( int i ) {
System.out.print(“Value is ” + i );
}
}
//File Sub.java
public class Sub extends Base {
public void method (int j) {
System.out.print(“This value is ” +
j);
}
public void method (String s) {
System.out.print(“I was passed ” + s);
}
public static void main(String args[]) {
Base b1 = new Base();
Base b2 = new Sub();
b1.method(5);
b2.method(6);
}
}
What output results when the main method of the class Sub is run?
○A. Value is 5Value is 6
○B. This value is 5This value is 6
○C. Value is 5This value is 6
○D. This value is 5Value is 6
○E. I was passed 5I was passed 6
20. Take the following code fragment:
switch ( x ) {
case 100 : System.out.println(“One hundred”); break;
case 200 : System.out.println(“Two hundred”); break;
case 300 : System.out.println(“Three hundred”); break;
}
Which declarations of x will not cause a compiler error [Check all correct answers.]
A. byte x = 100 ;
B. short x = 200 ;
C. int x = 300 ;
D. long x = 400 ;
21. You are writing a set of classes related to cooking and have created your own exception hierarchy derived from java.lang.Exception as follows (note that both BitterException and SourException descend from BadTasteException):
Exception
+-- BadTasteException
+-- BitterException
+-- SourException
Your base class, BaseCook, has a method declared as follows:
int rateFlavor(Ingredient[] list) throws BadTasteException
A class, TexMexCook, derived from BaseCook has a method that overrides BaseCook.rateFlavor(). Which of the following are legal declarations of the overriding method? [Check all correct answers.]
A. int rateFlavor(Ingredient[] list) throws BadTasteException
B. int rateFlavor(Ingredient[] list) throws Exception
C. int rateFlavor(Ingredient[] list) throws BitterException
D. int rateFlavor(Ingredient[] list)
22. Which of the following statements are true? [Check all correct answers.]
A. A final class must be instantiated
B. A final class must contain at least one final method.
C. A final class must contain at least one final data field.
D. A final class may not be extended.
E. None of the above.
23. What does the following code print?
public class A
{
static int x;
public static void main (String[] args) {
A that1 = new A();
A that2 = new A();
that1.x = 5;
that2.x = 1000;
x = -1;
System.out.println(x);
}
}
○A. 0
○B. 5
○C. 1000
○D. –1
24. Take the following code fragment with a break to a labeled statement:
int i, j;
lab: for ( i = 0 ; i <
6 ; i++) {
for ( j = 5 ; j > 2 ; j-- ) {
if ( i ==j ) {
System.out.print(“ ” + j ) ;
break lab;
}
}
}
What will the printed output be?
○A. 3 4 5
○B. 3 4
○C. 3
25. Assuming that the file file.txt is not available, what be the output of the following code?
import java.io.*;
public class ExTest{
public static void main (String argv[] ){
ExTest et = new ExTest();
System.out.println(et.test());
}
public int test() {
try{
throw new IOException();
} catch (IOException ioe){
System.out.println("IOException");
return 2;
} catch (Exception e){
System.out.println("Exception");
return 1;
} finally{
System.out.println("finally");
}
}
}
○A. Exception
1
○B. Exception
finally
1
○C. IOException
2
○D. IOException
finally
2
26. Which of the following signatures are valid for the main() method entry point of an application [Check all correct answers.]
A. public static void main()
B. public static void main (String arg[])
C. public void main (String [] arg)
D. public static void main (String [] args)
E. public static int main (String [] arg)
27. Consider the following line of code:
int [] x = new int[25];
After execution, which statements are true? [Check all correct answers.]
A. x[24] is 0
B. x[24] is undefined
C. x[25] is 0
D. x[0] is null
E. x.length is 25
28. Which one line in the following code will not compile?
1. byte b = 5;
2. char c = ‘5’;
3. short s = 55;
4. int i = 555;
5. float f = 555.5f;
6. b = s;
7. i = c;
8. if ( f > b )
9.
f = i;
○A. Line 1
○B. Line 2
○C. Line 3
○D. Line 4
○E. Line 5
○F. Line 6
○G. Line 7
○H. Line 8
○I. Line 9
29. Will the following code compile?
byte b = 2;
byte b1=3;
b=b * b1;
○A. Yes
○B. No
30. What results from attempting to compile and run the following code?
1.
public
class Conditional {
2.
public static void main ( String args[]) {
3.
int x = 4;
4.
System.out.println(“value is ” +
((x>4)? 99.99: 9));
5.
}
6. }
○A. The output: value is 99.99
○B. The output: value is 9.0
○C. A compiler error at line 4
31. Which of the following operations might throw an ArithmeticException?
○A. +
○B. –
○C. *
○D. /
○E. None of these
32. You have been given a design document for a veterinary registration system for implementation in Java. It states:
“A pet has an owner, a registration date, and a vaccination-due date. A cat is a pet that has a flag indicating whether it has been neutered, and a textual description of its markings.”
Given that the Pet class has already been defined, which of the following fields would be appropriate for inclusion in the Cat class as members? (Choose all that apply.)
A. Pet thePet;
B. Date registered;
C. Date vaccinationDue;
D. Cat theCat;
E. boolean neutered;
F. String markings;
33. Given the following switch statement, select the correct resulting value of the variable number with an input value of number = 1
switch (number) {
case 0: number += 2;
break;
case 1: number *=2;
case 2: number +=3;
break;
case 3: number /=2;
break;
default number = 0;
}
○A. 0
○B. 1
○C. 2
○D. 3
○E. 4
○F. 5
34. Given interfaces called Flyer and Sailor, which of the following code samples illustrates using an interface for a data type?
○A. public class Bird implements Flyer {
○B. Flyer bird;
○C. public class RowMan implements Sailor {
○D. int Flyer;
35. A variable, method, or class has ________ accessibility if it does not have an explicit protection modifier as part of its declaration.
○A. private
○B. protected
○C. default
○D. public
36. Overloaded methods all have the same ________.
○A. Number of arguments
○B. Method signature
○C. Type of arguments
○D. Name
37.
Which
method corresponds to the following method call?
myPerson.printValues (100, 147.7F,
"lavender");
○A. public void printValues (int pantSize, float ageInYears)
○B. public void printValues (pantSize, float ageInYears, favoriteColor)
○C. public void printValues (int pantSize, float ageInYears, String favoriteColor)
○D. public void printValues (float ageInYears, String favoriteColor, int pantSize)
38.
Which
of the following statements is true about constructors?
○A. Constructors return an int type.
○B. Constructor names might not match the Class name.
○C. Constructors allow you to specify which variables are initialized.
○D. Constructors are declared with return type void
39. What is the result of the following code segment?
10. public boolean a() { System.out.print("a"); return true; }
11. public boolean b() { System.out.print("b"); return false; }
12. public boolean c() { System.out.print("c"); return false; }
13. public void op() {
15. System.out.println( ( a() || b() ) || c() );
16. }
○A. true
○B. false
○C. atrue
○D. afalse
○E. abctrue
○F. abfalse
40. What is the result of the following code segment?
10. int i=3, j=0, result=1;
11. result += i-- * --j ;
12. System.out.println( result );
○A. 0
○B. –1
○C. –2
○D. –3
○E. Compilation fails
○F. An exception is thrown at runtime
41. Which is a reserved word in the Java programming language?
○A. goto
○B. local
○C. inner
○D. branch
○E. reference
42. When is the object created on line 8 eligible for garbage collection?
7. Dog [] da = new Dog[2];
8. Dog clover = new Dog();
9. da[1] = clover;
10. clover = null;
11. da[1] = da[0];
12. Dog spot = new Dog();
○A. after line 9
○B. after line 10
○C. after line 11
○D. never in this code
43. What is the result of the following code segment?
1. int x = 7;
2. while (x) {
3. if ( x == 3 ) {
4. break;
5. }
6. x--;
7. }
8. System.out.println("x = " + x);
○A. x = 0
○B. x = 3
○C. x = 4
○D. x = 7
○E. Compilation fails
44. If a class contains at least one abstract method, it is a(n)_____________class.
○A. abstract
○B. inner
○C. concrete
○D. interface
45. All methods in an abstract class must be declared as abstract methods.
○A. True
○B. False
46. You can implement many interfaces, but you can only extend one class.
○A. True
○B. False
47. A(n)______________variable represents class-wide information
○A. instance
○B. class
○C. member
○D. global
48. The keyword_________________specifies that a variable is not modifiable after it is initialized.
○A. const
○B. finalize
○C. final
○D. static
49. A method declared static cannot access_______________class members directly.
○A. non-static
○B. static
○C. public
○D. private
50. If you declare an array int [] a= new int[10] and try to print a[10], what happens:
○A. Compiler error (Possible reference before assignment)
○B. Code compiles but ArrayIndexOutOfBoundsException is thrown at runtime
○C. The value 0 will be printed
Short Answer( /6)
51. Why is main declared as static? (/2)
52. Given the following code, what do you think will be the output produced from running Test? Explain your reasoning behind this output? (/4)
//_______File A.java_________
class A {
void m() {
System.out.println("A's m");;
}
static void m1() {
System.out.println("A's m1");;
}
}
//_______File B.java_________
class B extends A {
void m() {
System.out.println("B's m");
}
static void m1() {
System.out.println("B's m1");
}
}
//_______File Test.java_________
public class Test {
public static void main (String[] args) {
B b = new B();
b.m();
b.m1();
A a = new B();
a.m();
a.m1();
}
}