From QuizSite

Additional text to be added later.



Question 1.
What happens if you try to compile code that looks like this:
class MyString extends String {

}

The code compiles successfully.
The code does not compile because you have not defined a main() method.
The code does not compile because String is abstract.
The code does not compile because String is final


Question 2.
Which word is not a Java keyword?

integer
double
float
default


Question 3.
Which keyword is not used to control access to a class member?

public
protected
private
default


Question 4.
Identify the keywords from this list:
abstract class    
double   Boolean

class, double, Boolean
class, double
abstract, double, class
abstract, class, double, Boolean


Question 5.
What is the result of attempting to compile and run the following code?
class ClassA { 
  public static void main (String[] args) { 
    ClassB b = new ClassB(); 
  }
  ClassA (int i) { 

  } 
} 
class ClassB extends ClassA { 

}

The code does not compile because the ClassA class does not define a no-args constructor.
The code does not compile because the ClassB class does not define a no-args constructor.
The code does not compile because there is no code in the ClassA(int i).
The code compiles and runs successfully.


Question 6.
What is the smallest integer data types you could use to hold:

a byte
a short
an int
a long


Question 7.
The range of values for the integer data types is:

-215 to 215-1
-2(number of bits - 1) to 2(number of bits - 1)
-231 to 231
-2(number of bits - 1) to 2(number of bits - 1) - 1


Question 8.
What is the result of trying to compile and run this program?
class ClassA { 
  int instVar1 = 1;
  int instVar2; 
  public static void main(String[] args) { 
    int localVar3 = 3;
    System.out.println(instVar1 + instVar2 + localVar3); 
  } 
}

4
0
The code does not compile because localVar is not initialized correctly.
The code does not compile because instVar2 is not initialized at all.


Question 9.
How can you initialize an array of three Boolean values to all true?

boolean[] b = new boolean[3];
boolean[] b = {true, true, true};
boolean[3] b = {true, true, true};
boolean[] b = new boolean[3]; b = {true, true, true};


Question 10.
What will be the result of this expression?
5 & 2 

0
2
5
7


Question 11.
What will be the result of this expression:
10 | 2 

0
2
10
14


Question 12.
The legal expression for an if statement is

an integer
a boolean
either an integer or a boolean
none of the above


Question 13.
Given the following code snippet:
char c = 'a';
switch(c) { 
  case 'a': System.out.println("a"); break;
  default: System.out.println("default"); 
}
What will happen if you attempt to compile and run the code that includes this snippet?

The code will not compile because the switch statement does not have a legal expression.
The code will compile and run but nothing will be written to the standard output.
The code will compile and run and the letter "a" will be written to the standard output.
The code will compile and run and the word "default" will be written to the standard output.


Question 14.
Given the following code snippet:
int myInt = 3;
if (myInt < 5)
  if (myInt < 3)
    System.println.out("< 3"); 
else 
  if (myInt > 2) 
    System.out.println("> 2"); 
else 
  System.out.println("other');

"< 3"
"> 2"
"other"
nothing


Question 15.
What type of line of code can have a label?

any line of code
only lines of code associated with a loop (just before, at the loop, just after)
only the line of code at the start of a loop
only the line of code defining the most outer loop


Question 16.
Why is this code illegal?
class A {
  public static void main(String[] args) { 
    try { 
      System.out.println("Hello!"); 
    } 
  } 
}

You cannot have a try block without a catch and/or finally
Code that does not throw an exception cannot be in a try block
The method main() must always throw something if the try block is used without a catch.


Question 17.
What is the result of attempting to compile and run this program?
class Over {
  public static void main (String[] args) {
    Under u = new Under(); 
    u.test(); 
  } 
  int test() { 
    System.out.println("Over"); 
    return 1; 
  } 
}

class Under extends Over { 
  short test() { 
    super.test(); 
    System.out.println("Under"); 
    return 1; 
  } 
}

The code does not compile.
This code compiles and runs and displays Over followed by Under.
This code compiles and runs and displays Under followed by Over.


Question 18.
What Math methods, invoked like this:
Math.method(x);
would return the value -5 given the value of x to be -4.5?

round()
ceil()
floor()
all of the above
round() and floor() only


Question 19.
Given this line of code:
String s = "Penguin";
What will be assigned to c if you execute:
char c = s.charAt(6)

'n'
'i'
Nothing will be assigned because charAt() will respond with a StringIndexOutOfBoundException


Question 20.
What do you expect the output to be for the following program?
class Str { 
  public static void main(String[] args) { 
    String s = new String("HELLO"); 
    String t; t = s.toUpperCase(); 
    if (s == t) System.out.println("equals"); 
    else        System.out.println("not equals"); 
  } 
}

equals
not equals


Question 21.
What do you expect the output to be for the following program?
class Str { 
  public static void main(String[] args) { 
    String s = new String("Hello"); 
    String t; t = s.toUpperCase(0; 
    if (s == t) System.out.println("equals"); 
    else        System.out.println("not equals"); 
  } 
}

equals
not equals


Question 22.
Imagine the following lines of code:
String s = "Hello"; 
String t = s; 
s += " world"; 
if (s == t) System.out.println("equals"); 
else        System.out.println("not equals"); 

equals
not equals


Question 23.
Why might a thread be alive but not be the currently executing thread?

It is the only thread currently running.
It has been suspended.
It has been resumed.
It has been notified.
All of the above.


Question 24.
If you have created two threads, one with a priority of Thread.MAX_PRIORITY and one with a normal, default priority, then which of these statements are true?

The thread with the normal priority will definitely not run until the thread with the maximum priority ends.
The thread with the normal priority will never run, even after the thread with the maximum priority ends.
Neither of the above is true.


Question 25.
The wait() method is defined in class:

Thread
Applet
Object
Runnable


Question 26.
You can set a thread's priority

When you first create a thread
At any time after you create a thread
All of the above


Question 27.
You should place all of your low-level graphics rendering code in:

update()
paint()
init()
repaint()


Question 28.
The AWT passes your paint() method an instance of class:

Thread
Applet
Graphics
Component


Question 29.
(Java 1.0.2) To identify the object where an event first occurred from an event handler, you can look at which field in the Event object?

component
target
evt
id


Question 30.
(Java 1.0.2) If the user hits the Enter key when typing in a TextField object, which event(s) does this TextField object receive by default?

handleEvent()
action()
both handleEvent() and action()
neither of these


Question 31.
The handleEvent() method takes which parameters?

Event and Object
Event
Object
Component


Question 32.
If you do not handle an event in action(), you should return:

false
true
null
0


Question 33.
Which of these defines a valid main() method?

public static void main(String args[]) { }
public static void main(String[]) { }
public static void main(String[] args)
public static void main(args)


Question 34.
How can you access the word "blue" from the following invocation of main():
java red blue green blue red

args[1]
args[3]
args[2]
args[4]
both args[1] and args[3]


Question 35.
To retrieve a parameter named lastname you could write code for your applet that could look like:

String s = getName("lastname");
String s = parameter("lastname");
String s = getParameter("lastname");


Question 36.
If you tried to read a parameter value and a parameter with that name was not defined in the applet tag:

The runtime would throw an exception
The parameter's value would be null
The parameter's value would be an empty string.


Question 37.
To force a layout manager to re-layout the components in a container, you can invoke the container method named:

validate()
repaint()
layout()
update()


Question 38.
To create an object that others can access remotely, you must implement which interface?

Clonable
Throwable
RemoteObject
Remote


Question 39.
All methods in a remote interface must:

not return a value
throw a RuntimeException
throw a RemoteException
be static methods


Question 40.
When an applet obtains a reference to a remote object that reference is:

a stub representing the object
a copy of the object
a direct reference to the object
an instance of RemoteObject


Question 41.
A socket encodes:

a port number
an IP address
both a port number and an IP address
none of these


Question 42.
To wait for a client to request a connection, your server can use the class

Socket
ServerSocket
Server
URL


Question 43.
The ServerSocket's accept() method returns an object of type:

Socket
ServerSocket
Server
URL


Question 44.
When you create a new Socket instance using a constructor that takes a host address:

Java attempts to establish a connection over the Internet with the host.
Java starts a server running on the host.
Nothing special happens until you invoke the Socket's accept() method.


Question 45.
TCP is used to

Identify a machine on the Internet based on the dotted octet or domain name
Ensure packets arrive in the same order they are sent
Both previous choices are correct
None of the above


Question 46.
Which of the following class definitions defines a legal abstract class?

class Animal { 
  abstract void growl(); 
}


abstract Animal { 
  abstract void growl(); 
}


class abstract Animal {
  abstract void growl();
}


abstract class Animal {
  abstract void growl(); 
}


abstract class Animal { 
  abstract void growl() { 
    System.out.println("growl"); 
  }  
}



Question 47.
What is the proper way of defining a class named Key so that it cannot be subclassed?

class Key { }                       


abstract final class Key { }        


native class Key { }                


class Key {
final
}


final class Key { }                 



Question 48.
What will appear in the standard output when you run the Tester class?
class Tester {
  int var; 
  Tester (double var) { this.var = (int)var; } 
  Tester (int var) { this("hello"); } 
  Tester (String s) { this(); System.out.println(s); } 
  Tester () { System.out.println("good-bye"); } 
  public static void main(String[] args) {
    Tester t = new Tester(5); 
  }  
}

nothing
hello
5
hello followed by good-bye
good-bye followed by hello


Question 49.
There are a number of labels in the source code below. These are labeled a through j. Which label identifies the earliest point where, after the line has executed, the object referred to by the variable first may be garbage collected?
class Riddle {
  public static void main(String[] args) {
    String first, second; String riddle; 
    if (args.length < 2) return; 

a:  first = new String(args[0]); 
b:  second = new String(args[1]); 
c:  riddle = "When is a " + first; 
d:  first = null; 
e:  riddle += " like a " + second + "?"; 
f:  second = null; 
  
g:  System.out.println(riddle); 

h:  args[0] = null; 
i:  args[1] = null; 

j: 

  }  
} 

d:
e:
h:
i:
j:


Question 50.
What are the range of values for a variable of type byte?

-27 to 27 - 1
0 to 28
-28 to 28
-27 to 27 - 1
-(28 - 1) to 28


Question 51.
What will happen when you compile and run this program:
class Array {
  public static void main(String[] args) {
    int length = 100; 
    int[] d = new int[length]; 
    for (int index = 0; index < length; index++) 
      System.out.println(d[index]); 
  } 
}

The code will not compile because the int[] array is not declared correctly.
The code will compile but will throw an IndexArrayOutOfBoundsException when it runs and nothing will appear in the standard output
The code will display the numbers 0 through 99 in the standard output, and then throw an IndexOutOfBoundsException
The code will compile but the println() method will throw a NoSuchMethodException
This code will work fine and display 100 zeroes in the standard output.


Question 52.
Given these class definitions
class Superclass { }
class Subclass1 extends Superclass { }
and these objects
Superclass a = new Superclass();
Subclass1 b = new Subclass1();
which of the following explains the result of the statement:
a = b

Illegal at compile time
Legal at compile time but possibly illegal at runtime
Definitely legal at runtime


Question 53.
How can you change the break statement below so that it breaks out of both the inner and middle loops and continues with the next iteration of the outer loop?
outer: for (int x = 0; x < 3; x++) {
  middle: for (int y = 0; y < 3; y++) {
    inner: for (int z = 0; z < 3; z++) {
      if (arr(x, y, z) == targetValue) 
        break; 
    } 
  }  
}

break inner
break middle
break outer
continue
continue middle


Question 54.
Given these class definitions
class Superclass { }
class Subclass1 extends Superclass { }
and these objects
Superclass a = new Superclass();
Subclass1 b = new Subclass1();
which of the following explains the result of the statement:
b = a;

Illegal at compile time
Legal at compile time but possibly illegal at runtime
Definitely legal at runtime


Question 55.
Given this code snippet:
double a = 90.7;
double b = method(a);
System.out.println(b);
If this snippet displays 90 in the standard output, what Math method did method() invoke?

abs()
min()
floor()
round()
ceil()


Question 56.
Which expressions are illegal?

(true & true)
(4 & 5)
(int myInt = 0 > 3)
float myFloat = 40.0;
boolean b = (boolean)99;


Question 57.
When you invoke repaint() for a Component, the AWT package class which Component method?

repaint()
update()
paint()
draw()
show()


Question 58.
What does the following line of code do?
TextField tf = new TextField(30);

This code is illegal, there is no such constructor for TextField
Creates a TextField object than can hold 30 rows, but since it is not initialized to anything, it will always be empty
Creates a TextField object that can hold 30 columns, but since it is not initialized to anything, it will always be empty
Creates a TextField object that can hold 30 rows of text
Creates a new TextField object that is 30 columns of text


Question 59.
Given these code snippets
Boolean b1 = new Boolean(true);
Boolean b2 = new Boolean(true);
Which expressions are legal Java expressions that return true?

b1 == b2
b1.equals(b2)
b1 & b2
b1 | b2
b1 && b2
b1 || b2


Question 60.
What will happen when you attempt to compile and run the following program by passing the Test class to the Java interpreter?
class Test {
  public static void main() {
    System.out.println("hello"); 
  } 
}

The program does not compile because main() is not defined correctly.
The program compiles but when you try to run the interpreter complains that it cannot find the main() method it needs to run
The program compiles but you cannot run it because the class is not declared as public
The program compiles and runs without an error but does not display anything in the standard output
The program compiles and displays hello in the standard output when you run it


Question 61.
Returning a value of false in Java 1.0.2 from an event handler:

passes that event up the container hierarchy
stops that event from being passed up the container hierarchy
has no effect on whether the event is passed up the container hierarchy


Question 62.
Which statements about garbage collection are true?

You can directly free memory allocated by an object
You can directly run the garbage collector whenever you want to
The garbage collector informs your object when it is about to be garbage collected
The garbage collector reclaims an object's memory as soon as it becomes a candidate for garbage collection
The garbage collector runs in low-memory situations


Question 63.
The && operator works with which data types?

int
long
double
boolean
float


Question 64.
Which answers provide a valid definition for a main() method that you can use to invoke a class?

void main(String[] args) { }
static public void main(String args[]) { }
public static void main(String[] args) { }
public static int main(String[] args) { }
public static void main(String args[]) { }


Question 65.
The valid arguments for a switch statement are:

int
float
char
boolean
Object


Question 66.
Select all valid Java keywords from those presented below

zero
instanceof
implements
NULL
sizeof
goto


Question 67.
What string will s and t contain after this code snippet executes:
String s = "Here's to you ";
String t = s + "Mrs. "; 
String t += "Robinson."; 

s = "Here's to you "
t = "Here's to you Mrs. Robinson."

s = "Here's to you Mrs. "
t = "Here's to you Mrs. Robinson."

s = "Here's to you Mrs. "
t = "Mrs. Robinson."

s = null
t = "Here's to you Mrs. Robinson."

s = null
t = "Robinson."


Question 68.
What is the results of attempting to compile and execute a program containing the following snippet?
Object b1 = new Boolean(false); 
Object b2 = new Boolean(false); 
System.out.println(b1.equals(b2));

This code will not compile because you cannot assign a Boolean to an Object
This code will not compile because you cannot test for equality of two Boolean values using equals()
This code compiles and when it runs it displays the word true
This code compiles and when it runs it displays the word false
This code runs but throws NoSuchMethodException


Question 69.
In the following code snippet:
String s = "Maybe "; 
s += " not"; 
Which of the following statements are true?

s contains "Maybe  not" at the end.
This code produces a String object that becomes a candidate for garbage collection
Only one String object is created
This code produces an exception because a String object cannot be modified once it is created


Question 70.
The trim() method defined in class String:

Can remove all leading white space
Can remove all trailing white space
Can trim the String to a substring specified as a parameter
Can trim the String to start and end indexes specified as parameters


Question 71.
Which method do you have to define if you implement the Runnable interface?

start()
runnable()
run()
init()
main()


Question 72.
Which operators are overloaded for String objects?

-
+=
>>
&
none of the above