Você está na página 1de 58

q

prompt
in which package can the swing components be found:
type
single
option
com.sun.java.swing.*
option
java.awt.swing.*
option
javax.swing.*
option
java.swing.*
answer
3
explain
swing was placed in the javax standard language extension with the <b>final</b>
release of the java 2 platform.
q
prompt
what is the correct ordering for the import, class and package declarations when
found in a single file?
type
single
option
package, import, class
option
class, import, package
option
import, package, class
option
package, class, import
answer
1
explain
this is my explanation for question 2
endexplain
q
prompt
which methods can be legally applied to a string object?
type
multiple
option
equals(string)
option
equals(object)
option
trim()
option
round()
option
tostring()
answer
1
answer
2
answer
3
answer
5
q
prompt
what is the parameter specification for the public static void main method?
type
multiple
option
string args []
option
string [] args
option
strings args []
option
string args
answer
1
answer
2
q
prompt
what does the zeroth element of the string array passed to the public static void
main method contain?
type
multiple
option
the name of the program
option
the number of arguments
option
the first argument if one is present
answer
3
q
prompt
which of the following are java keywords?
type
multiple
option
goto
option
malloc
option
extends
option
false
option
sizeof
option
friend
answer
3
q
prompt
what will be the result of compiling the following code:
code
public class test {
public static void main (string args []) {
int age;
age = age + 1;
system.out.println("the age is " + age);
}
}
endcode
type
single
option
compiles and runs with no output
option
compiles and runs printing out the age is 1
option
compiles but generates a runtime error
option
does not compile
option
compiles but generates a compile time error
answer
4
q
prompt
which of these is the correct format to use to create the literal char value a?
type
multiple
option
'a'
option
"a"
option
new character(a)
option
\000a
answer
1
q
prompt
what is the legal range of a byte integral type?
type
single
option
0 - 65, 535
option
(-128) - 127
option
(-32,768) - 32,767
option
(-256) - 255
answer
2
q
prompt
which of the following is illegal:
type
single
option
int i = 32;
option
float f = 45.0;
option
double d = 45.0;
option
string a = 32;
answer
2
q
prompt
what will be the result of compiling the following code:
code
public class test {
static int age;
public static void main (string args []) {
age = age + 1;
system.out.println("the age is " + age);
}
}
endcode
type
single
option
compiles and runs with no output
option
compiles and runs printing out the age is 1
option
compiles but generates a runtime error
option
does not compile
option
compiles but generates a compile time error
answer
2
q
prompt
which of the following are correct?
type
multiple
option
128 >> 1 gives 64
option
128 >>> 1 gives 64
option
128 >> 1 gives -64
option
128 >>> 1 gives -64
answer
1
q
prompt
which of the following return true?
type
multiple
option
"john" == new string("john")
option
"john".equals("john")
option
"john" = "john"
option
"john".equals(new button("john"))
answer
2
q
prompt
which of the following do not lead to a runtime error?
type
multiple
option
"john" + " was " + " here"
option
"john" + 3
option
3 + 5
option
5 + 5.5
answer
1
answer
2
answer
3
answer
4
q
prompt
which of the following are so called "short circuit" logical operators?
type
multiple
option
&
option
||
option
&&
option
|
answer
2
answer
3
q
prompt
which of the following are acceptable?
type
multiple
option
object o = new button("a");
option
boolean flag = true;
option
panel p = new frame();
option
frame f = new panel();
option
panel p = new applet();
answer
1
answer
5
q
prompt
what is the result of compiling and running the following code:
code
public class test {
static int total = 10;
public static void main (string args []) {
new test();
}
public test () {
system.out.println("in test");
system.out.println(this);
int temp = this.total;
if (temp > 5) {
system.out.println(temp);
}
}
}
endcode
type
multiple
option
the class will not compile
option
the compiler reports and error at line 2
option
the compiler reports an error at line 9
option
the value 10 is one of the elements printed to the standard output
option
the class compiles but generates a runtime error
answer
4
q
prompt
which of the following is correct:
type
single
option
string temp [] = new string {"j" "a" "z"};
option
string temp [] = { "j " " b" "c"};
option
string temp = {"a", "b", "c"};
option
string temp [] = {"a", "b", "c"};
answer
4
q
prompt
what is the correct declaration of an abstract method that is intended to be
public:
type
single
option
public abstract void add();
option
public abstract void add() {}
option
public abstract add();
option
public virtual add();
answer
1
q
prompt
under what situations do you obtain a default constructor?
type
single
option
when you define any class
option
when the class has no other constructors
option
when you define at least one constructor
answer
2
q
prompt
which of the following can be used to define a constructor for this class, given
the following code:
code
public class test {
...
}
endcode
type
single
option
public void test() {...}
option
public test() {...}
option
public static test() {...}
option
public static void test() {...}
answer
2
q
prompt
which of the following are acceptable to the java compiler:
type
multiple
option
if (2 == 3) system.out.println("hi");
option
if (2 = 3) system.out.println("hi");
option
if (true) system.out.println("hi");
option
if (2 != 3) system.out.println("hi");
option
if (astring.equals("hello")) system.out.println("hi");
answer
1
answer
3
answer
4
answer
5
q
prompt
assuming a method contains code which may raise an exception (but not a
runtimeexception), what is the correct way for a method to indicate that it
expects the caller to handle that exception:
type
single
option
throw exception
option
throws exception
option
new exception
option
don't need to specify anything
answer
2
q
prompt
what is the result of executing the following code, using the parameters 4 and 0:
code
public void divide(int a, int b) {
try {
int c = a / b;
} catch (exception e) {
system.out.print("exception ");
} finally {
system.out.println("finally");
}
endcode
type
singe
option
prints out: exception finally
option
prints out: finally
option
prints out: exception
option
no output
answer
1
q
prompt
which of the following is a legal return type of a method overloading the
following method:
code

public void add(int a) {...}


endcode
type
single
option
void
option
int
option
can be anything
answer
3
q
prompt
which of the following statements is correct for a method which is overriding the
following method:
code

public void add(int a) {...}

endcode
type
single
option
the overriding method must return void
option
the overriding method must return int
option
the overriding method can return whatever it likes
answer
1
q
prompt
given the following classes defined in separate files, what will be the effect of
compiling and running this class test?
code
class vehicle {
public void drive() {
system.out.println("vehicle: drive");
}
}

class car extends vehicle {


public void drive() {
system.out.println("car: drive");
}
}

public class test {


public static void main (string args []) {
vehicle v;
car c;
v = new vehicle();
c = new car();
v.drive();
c.drive();
v = c;
v.drive();
}
}
endcode
type
single
option
generates a compiler error on the statement v= c;
option
generates runtime error on the statement v= c;
option
prints out:
list
vehicle: drive
car: drive
car: drive
endlist
option
prints out:
list
vehicle: drive
car: drive
vehicle: drive
endlist
answer
3
q
prompt
where in a constructor, can you place a call to a constructor defined in the super
class?
type
single
option
anywhere
option
the first statement in the constructor
option
the last statement in the constructor
option
you can't call super in a constructor
answer
2
q
prompt
which variables can an inner class access from the class which encapsulates it?
type
multiple
option
all static variables
option
all final variables
option
all instance variables
option
only final instance variables
option
only final static variables
answer
1
answer
2
answer
3
q
prompt
what class can an inner class extend:
type
single
option
the top level class
option
the object class
option
any class or interface (other than the outer class)
option
it must implement an interface
answer
3
q
prompt
in the following code, which is the earliest statement, where the object
originally held in e, may be garbage collected:
code
1. public class test {
2. public static void main (string args []) {
3. employee e = new employee("bob", 48);
4. e.calculatepay();
5. system.out.println(e.printdetails());
6. e = null;
7. e = new employee("denise", 36);
8. e.calculatepay();
9. system.out.println(e.printdetails());
10. }
11. }
endcode
type
single
option
line 10
option
line 11
option
line 7
option
line 8
option
never
answer
3
q
prompt
what is the name of the interface that can be used to define a class that can
execute within its own thread?
type
single
option
runnable
option
run
option
threadable
option
thread
option
executable
answer
1
q
prompt
what is the name of the method used to schedule a thread for execution?
type
single
option
init();
option
start();
option
run();
option
resume();
option
sleep();
answer
2
q
prompt
which methods may cause a thread to stop executing?
type
multiple
option
sleep();
option
stop();
option
yield();
option
wait();
option
notify();
option
notifyall()
option
synchronized()
answer
1
answer
2
answer
3
answer
4
q
prompt
which of the following would create a text field able to display 10 characters
(assuming a fixed size font) displaying the initial string "hello":
:
type
single
option
new textfield("hello", 10);
option
new textfield("hello");
option
new textfield(10);
option
new textfield();
answer
1
q
prompt
which of the following methods are defined on the graphics class:
type
multiple
option
drawline(int, int, int, int)
option
drawimage(image, int, int, imageobserver)
option
drawstring(string, int, int)
option
add(component);
option
setvisible(boolean);
option
setlayout(object);
answer
1
answer
2
answer
3
q
prompt
which of the following layout managers honours the preferred size of a component:
type
multiple
option
cardlayout
option
flowlayout
option
borderlayout
option
gridlayout
answer
2
q
prompt
given the following code what is the effect of a being 5:
code
public class test {
public void add(int a) {
loop: for (int i = 1; i < 3; i++){
for (int j = 1; j < 3; j++) {
if (a == 5) {
break loop;
}
system.out.println(i * j);
}
}
}
}
endcode
type
single
option
generate a runtime error
option
throw an arrayindexoutofboundsexception
option
print the values: 1, 2, 2, 4
option
produces no output
answer
4
q
prompt
what is the effect of issuing a wait() method on an object
type
single
option
if a notify() method has already been sent to that object then it has no effect
option
the object issuing the call to wait() will halt until another object sends a
notify() or notifyall() method
option
an exception will be raised
option
the object issuing the call to wait() will be automatically synchronized with any
other objects using the receiving object.
answer
2
q
prompt
the layout of a container can be altered using which of the following methods:
type
multiple
option
setlayout(alayoutmanager);
option
addlayout(alayoutmanager);
option
layout(alayoutmanager);
option
setlayoutmanager(alayoutmanager);
answer
1
q
prompt
using a flowlayout manager, which is the correct way to add elements to a
container:
type
single
option
add(component);
option
add("center", component);
option
add(x, y, component);
option
set(component);
answer
1
q
prompt
given that a button can generate an actionevent which listener would you expect to
have to implement, in a class which would handle this event?
type
single
option
focuslistener
option
componentlistener
option
windowlistener
option
actionlistener
option
itemlistener
answer
4
q
prompt
which of the following, are valid return types, for listener methods:
type
single
option
boolean
option
the type of event handled
option
void
option
component
answer
3
q
prompt
assuming we have a class which implements the actionlistener interface, which
method should be used to register this with a button?
type
single
option
addlistener(*);
option
addactionlistener(*);
option
addbuttonlistener(*);
option
setlistener(*);
answer
2
q
prompt
in order to cause the paint(graphics) method to execute, which of the following is
the most appropriate method to call:
type
single
option
paint()
option
repaint()
option
paint(graphics)
option
update(graphics)
option
none - you should never cause paint(graphics) to execute
answer
2
q
prompt
which of the following illustrates the correct way to pass a parameter into an
applet:
type
single
option
<applet code=test.class age=33 width=100 height=100>
option
<param name=age value=33>
option
<applet code=test.class name=age value=33 width=100 height=100>
option
<applet test 33>
answer
2
q
prompt
which of the following correctly illustrate how an inputstreamreader can be
created:
type
multiple
option
new inputstreamreader(new fileinputstream("data"));
option
new inputstreamreader(new filereader("data"));
option
new inputstreamreader(new bufferedreader("data"));
option
new inputstreamreader("data");
option
new inputstreamreader(system.in);
answer
1
answer
5
q
prompt
what is the permanent effect on the file system of writing data to a new
filewriter("report"), given the file report already exists?
type
single
option
the data is appended to the file
option
the file is replaced with a new file
option
an exception is raised as the file already exists
option
the data is written to random locations within the file
answer
2
q
prompt
what is the effect of adding the sixth element to an arraylist created in the
following manner:
code

new arraylist(5);

endcode
type
single
option
an indexoutofbounds exception is raised.
option
the arraylist grows to accomodate the sixth element
option
the new element will be lost
option
nothing, the arraylist will have grown when the fifth element was added
answer
2
q
prompt
which of the following defines an interface which acts as the root of all
collection classes (except maps)?
type
single
option
collections
option
collection
option
list
option
arrays
option
array
answer
2
q
prompt
which of the following defines an interface that allows a key to be associated
with a value:
type
single
option
map
option
set
option
list
option
collection
option
enumeration
answer
1
q
prompt
what is the result of executing the following code when the value of x is 2:
code

switch (x) {
case 1:
system.out.println(1);
case 2:
case 3:
system.out.println(3);
case 4:
system.out.println(4);
}

endcode
type
single
option
nothing is printed out
option
the value 3 is printed out
option
the values 3 and 4 are printed out
option
the values 1, 3 and 4 are printed out
answer
3
q
prompt
consider the following example:
code
class first {
public first (string s) {
system.out.println(s);
}
}
public class second extends first {
public static void main(string args []) {
new second();
}
}
endcode
type
single
what is the result of compiling and running the second class?
option
nothing happens
option
a string is printed to the standard out
option
an instance of the class first is generated
option
an instance of the class second is created
option
an exception is raised at runtime stating that there is no null parameter
constructor in class first.
option
the class second will not compile as there is no null parameter constructor in the
class first
answer
6
q
prompt
what is the result of executing the following fragment of code:
code
boolean flag = false;
if (flag = true) {
system.out.println("true");
} else {
system.out.println("false");
}
endcode
type
single
option
true is printed to standard out
option
false is printed to standard out
option
an exception is raised
option
nothing happens
answer
1
q
prompt
consider the following classes. what is the result of compiling and running this
class?
code
public class test {
public static void test() {
this.print();
}
public static void print() {
system.out.println("test");
}
public static void main(string args []) {
test();
}
}
endcode
type
multiple
option
the string test is printed to the standard out.
option
a runtime exception is raised stating that an object has not been created.
option
nothing is printed to the standard output.
option
an exception is raised stating that the method test cannot be found.
option
an exception is raised stating that the variable this can only be used within an
instance.
option
the class fails to compile stating that the variable this is undefined.
answer
6
q
prompt
examine the following class definition:
code
public class test {
public static void test() {
print();
}
public static void print() {
system.out.println("test");
}
public void print() {
system.out.println("another test");
}
}
endcode
type
single
what is the result of compiling this class:
option
a successful compilation.
option
a warning stating that the class has no main method.
option
an error stating that there is a duplicated method.
option
an error stating that the method test() will call one or other of the print()
methods.
answer
3
q
prompt
what is the result of compiling and executing the following java class:
code
public class threadtest extends thread {
public void run() {
system.out.println("in run");
suspend();
resume();
system.out.println("leaving run");
}
public static void main(string args []) {
(new threadtest()).start();
}
}
endcode
type
single
option
compilation will fail in the method main.
option
compilation will fail in the method run.
option
a warning will be generated for method run.
option
the string "in run" will be printed to standard out.
option
both strings will be printed to standard out.
option
nothing will happen.
answer
4
q
prompt
given the following sequence of java statements, which of the following options
are true:
code
1. stringbuffer sb = new stringbuffer("abc");
2. string s = new string("abc");
3. sb.append("def");
4. s.append("def");
5. sb.insert(1, "zzz");
6. s.concat(sb);
7. s.trim();
endcode
type
multiple
option
the compiler would generate an error for line 1.
option
the compiler would generate an error for line 2.
option
the compiler would generate an error for line 3.
option
the compiler would generate an error for line 4.
option
the compiler would generate an error for line 5.
option
the compiler would generate an error for line 6.
option
the compiler would generate an error for line 7.
answer
4
answer
6
q
prompt
what is the result of executing the following java class:
code
import java.awt.*;

public class frametest extends frame {


public frametest() {
add (new button("first"));
add (new button("second"));
add (new button("third"));
pack();
setvisible(true);
}
public static void main(string args []) {
new frametest();
}
}
endcode
type
single
option
nothing happens.
option
three buttons are displayed across a window.
option
a runtime exception is generated (no layout manager specified).
option
only the "first" button is displayed.
option
only the "second" button is displayed.
option
only the "third" button is displayed.
answer
6
q
prompt
consider the following tags and attributes of tags, which can be used with the
<aaplet> and </applet> tags?
code
1. codebase
2. alt
3. name
4. class
5. javac
6. horizontalspace
7. verticalspace
8. width
9. param
10. jar
endcode
type
multiple
option
line 1, 2, 3
option
line 2, 5, 6, 7
option
line 3, 4, 5
option
line 8, 9, 10
option
line 8, 9
answer
1
answer
5
q
prompt
which of the following is a legal way to construct a randomaccessfile:
type
single
option
randomaccessfile("data", "r");
option
randomaccessfile("r", "data");
option
randomaccessfile("data", "read");
option
randomaccessfile("read", "data");
answer
1
q
prompt
carefully examine the following code, when will the string "hi there" be printed?
code
public class statictest {
static {
system.out.println("hi there");
}
public void print() {
system.out.println("hello");
}
public static void main(string args []) {
statictest st1 = new statictest();
st1.print();
statictest st2 = new statictest();
st2.print();
}
}
endcode
type
single
option
never.
option
each time a new instance is created.
option
once when the class is first loaded into the java virtual machine.
option
only when the static method is called explicitly.
answer
3
q
prompt
what is the result of the following program:
code
public class test {
public static void main (string args []) {
boolean a = false;
if (a = true)
system.out.println("hello");
else
system.out.println("goodbye");
}
}
endcode
type
single
option
program produces no output but terminates correctly.
option
program does not terminate.
option
prints out "hello"
option
prints out "goodbye"
answer
3
q
prompt
examine the following code, it includes an inner class, what is the result:
code
public final class test4 {
class inner {
void test() {
if (test4.this.flag); {
sample();
}
}
}
private boolean flag = true;
public void sample() {
system.out.println("sample");
}
public test4() {
(new inner()).test();
}
public static void main(string args []) {
new test4();
}
}
endcode
type
single
option
prints out "sample"
option
program produces no output but terminates correctly.
option
program does not terminate.
option
the program will not compile
answer
1
q
prompt
carefully examine the following class:
code
public class test5 {
public static void main (string args []) {
/* this is the start of a comment
if (true) {
test5 = new test5();
system.out.println("done the test");
}
/* this is another comment */
system.out.println ("the end");
}
}
endcode
type
single
option
prints out "done the test" and nothing else.
option
program produces no output but terminates correctly.
option
program does not terminate.
option
the program will not compile.
option
the program generates a runtime exception.
option
the program prints out "the end" and nothing else.
option
the program prints out "done the test" and "the end"
answer
6
q
prompt
what is the result of compiling and running the following applet:
code
import java.applet.applet;
import java.awt.*;

public class sample extends applet {


private string text = "hello world";
public void init() {
add(new label(text));
}
public sample (string string) {
text = string;
}
}

it is accessed form the following html page:

<html>
<title>sample applet</title>
<body>
<applet code="sample.class" width=200 height=200></applet>
</body>
</html>
endcode
type
single
option
prints "hello world".
option
generates a runtime error.
option
does nothing.
option
generates a compile time error.
answer
2
q
prompt
what is the effect of compiling and (if possible) running this class:
code
public class calc {
public static void main (string args []) {
int total = 0;
for (int i = 0, j = 10; total > 30; ++i, --j) {
system.out.println(" i = " + i + " : j = " + j);
total += (i + j);
}
system.out.println("total " + total);
}
}
endcode
type
single
option
produce a runtime error
option
produce a compile time error
option
print out "total 0"
option
generate the following as output:
list
i = 0 : j = 10
i = 1 : j = 9
i = 2 : j = 8
total 30
endlist
answer
3
qend
q
prompt
which methods can be legally applied to a string object?
type
multiple
option
equals(string)
option
equals(object)
option
trim()
option
round()
option
tostring()
answer
1
answer
2
answer
3
answer
5
q
prompt
what is the parameter specification for the public static void main method?
type
multiple
option
string args []
option
string [] args
option
strings args []
option
string args
answer
1
answer
2
q
prompt
what does the zeroth element of the string array passed to the public static void
main method contain?
type
multiple
option
the name of the program
option
the number of arguments
option
the first argument if one is present
answer
3
q
prompt
which of the following are java keywords?
type
multiple
option
goto
option
malloc
option
extends
option
false
answer
3
q
prompt
what will be the result of compiling the following code:
code
public class test {
public static void main (string args []) {
int age;
age = age + 1;
system.out.println("the age is " + age);
}
}
endcode
type
single
option
compiles and runs with no output
option
compiles and runs printing out the age is 1
option
compiles but generates a runtime error
option
does not compile
option
compiles but generates a compile time error
answer
4
q
prompt
which of these is the correct format to use to create the literal char value a?
type
multiple
option
'a'
option
"a"
option
new character(a)
option
\000a
answer
1
q
prompt
what is the legal range of a byte integral type?
type
single
option
0 - 65, 535
option
(-128) - 127
option
(-32,768) - 32,767
option
(-256) - 255
answer
2
q
which of the following is illegal:
type
single
option
int i = 32;
option
float f = 45.0;
option
double d = 45.0;
answer
2
q
prompt
what will be the result of compiling the following code:
code
public class test {
static int age;
public static void main (string args []) {
age = age + 1;
system.out.println("the age is " + age);
}
}
endcode
type
single
option
compiles and runs with no output
option
compiles and runs printing out the age is 1
option
compiles but generates a runtime error
option
does not compile
option
compiles but generates a compile time error
answer
2
q
prompt
which of the following are correct?
type
multiple
option
128 >> 1 gives 64
option
128 >>> 1 gives 64
option
128 >> 1 gives -64
option
128 >>> 1 gives -64
answer
1
q
prompt
which of the following return true?
type
multiple
option
"john" == new string("john")
option
"john".equals("john")
option
"john" = "john"
option
"john".equals(new button("john"))
answer
2
q
prompt
which of the following do not lead to a runtime error?
type
multiple
option
"john" + " was " + " here"
option
"john" + 3
option
3 + 5
option
5 + 5.5
answer
1
answer
2
answer
3
answer
4
q
prompt
which of the following are so called "short circuit" logical operators?
type
multiple
option
&
option
||
option
&&
option
|
answer
2
answer
3
q
prompt
which of the following are acceptable?
type
multiple
option
object o = new button("a");
option
boolean flag = true;
option
panel p = new frame();
option
frame f = new panel();
option
panel p = new applet();
answer
1
answer
5
q
prompt
what is the result of compiling and running the following code:
code
public class test {
static int total = 10;
public static void main (string args []) {
new test();
}
public test () {
system.out.println("in test");
system.out.println(this);
int temp = this.total;
if (temp > 5) {
system.out.println(temp);
}
}
}
endcode
type
multiple
option
the class will not compile
option
the compiler reports and error at line 2
option
the compiler reports an error at line 9
option
the value 10 is one of the elements printed to the standard output
option
the class compiles but generates a runtime error
answer
4
q
prompt
which of the following is correct:
type
single
option
string temp [] = new string {"j" "a" "z"};
option
string temp [] = { "j " " b" "c"};
option
string temp = {"a", "b", "c"};
option
string temp [] = {"a", "b", "c"};
answer
4
q
prompt
what is the correct declaration of an abstract method that is intended to be
public:
type
single
option
public abstract void add();
option
public abstract void add() {}
option
public abstract add();
option
public virtual add();
answer
1
q
prompt
under what situations do you obtain a default constructor?
type
single
option
when you define any class
option
when the class has no other constructors
option
when you define at least one constructor
answer
2
q
prompt
which of the following can be used to define a constructor for this class, given
the following code:
code
public class test {
...
}
endcode
type
single
option
public void test() {...}
option
public test() {...}
option
public static test() {...}
option
public static void test() {...}
answer
2
q
prompt
which of the following are acceptable to the java compiler:
type
multiple
option
if (2 == 3) system.out.println("hi");
option
if (2 = 3) system.out.println("hi");
option
if (true) system.out.println("hi");
option
if (2 != 3) system.out.println("hi");
option
if (astring.equals("hello")) system.out.println("hi");
answer
1
answer
3
answer
4
answer
5
q
prompt
assuming a method contains code which may raise an exception (but not a
runtimeexception), what is the correct way for a method to indicate that it
expects the
caller to handle that exception:
type
single
option
bash-3.00$
bash-3.00$
bash-3.00$
bash-3.00$ more questions1.1.txt
q
prompt
which colour is used to indicate instance methods in the standard "javadoc" format
documentation:
type
single
option
blue
option
red
option
purple
option
orange
answer
2
explain
in jdk 1.1 the variabels, methods and constructors are colour coded to
<i>simplify</i> their identification.
endexplain
q
prompt
what is the correct ordering for the import, class and package declarations when
found in a single file?
type
single
option
package, import, class
option
class, import, package
option
import, package, class
option
package, class, import
answer
1
explain
this is my explanation for question 2
endexplain
q
prompt
which methods can be legally applied to a string object?
type
multiple
option
equals(string)
option
equals(object)
option
trim()
option
round()
option
tostring()
answer
1
answer
2
answer
3
answer
5
q
prompt
what is the parameter specification for the public static void main method?
type
multiple
option
string args []
option
string [] args
option
strings args []
option
string args
answer
1
answer
2
q
prompt
what does the zeroth element of the string array passed to the public static void
main method contain?
type
multiple
option
the name of the program
option
the number of arguments
option
the first argument if one is present
answer
3
q
prompt
which of the following are java keywords?
type
multiple
option
goto
option
malloc
option
extends
option
false
answer
3
q
prompt
what will be the result of compiling the following code:
code
public class test {
public static void main (string args []) {
int age;
age = age + 1;
system.out.println("the age is " + age);
}
}
endcode
type
single
option
compiles and runs with no output
option
compiles and runs printing out the age is 1
option
compiles but generates a runtime error
option
does not compile
option
compiles but generates a compile time error
answer
4
q
prompt
which of these is the correct format to use to create the literal char value a?
type
multiple
option
'a'
option
"a"
option
new character(a)
option
\000a
answer
1
q
prompt
what is the legal range of a byte integral type?
type
single
option
0 - 65, 535
option
(-128) - 127
option
(-32,768) - 32,767
option
(-256) - 255
answer
2
q
which of the following is illegal:
type
single
option
int i = 32;
option
float f = 45.0;
option
double d = 45.0;
answer
2
q
prompt
what will be the result of compiling the following code:
code
public class test {
static int age;
public static void main (string args []) {
age = age + 1;
system.out.println("the age is " + age);
}
}
endcode
type
single
option
compiles and runs with no output
option
compiles and runs printing out the age is 1
option
compiles but generates a runtime error
option
does not compile
option
compiles but generates a compile time error
answer
2
q
prompt
which of the following are correct?
type
multiple
option
128 >> 1 gives 64
option
128 >>> 1 gives 64
option
128 >> 1 gives -64
option
128 >>> 1 gives -64
answer
1
q
prompt
which of the following return true?
type
multiple
option
"john" == new string("john")
option
"john".equals("john")
option
"john" = "john"
option
"john".equals(new button("john"))
answer
2
q
prompt
which of the following do not lead to a runtime error?
type
multiple
option
"john" + " was " + " here"
option
"john" + 3
option
3 + 5
option
5 + 5.5
answer
1
answer
2
answer
3
answer
4
q
prompt
which of the following are so called "short circuit" logical operators?
type
multiple
option
&
option
||
option
&&
option
|
answer
2
answer
3
q
prompt
which of the following are acceptable?
type
multiple
option
object o = new button("a");
option
boolean flag = true;
option
panel p = new frame();
option
frame f = new panel();
option
panel p = new applet();
answer
1
answer
5
q
prompt
what is the result of compiling and running the following code:
code
public class test {
static int total = 10;
public static void main (string args []) {
new test();
}
public test () {
system.out.println("in test");
system.out.println(this);
int temp = this.total;
if (temp > 5) {
system.out.println(temp);
}
}
}
endcode
type
multiple
option
the class will not compile
option
the compiler reports and error at line 2
option
the compiler reports an error at line 9
option
the value 10 is one of the elements printed to the standard output
option
the class compiles but generates a runtime error
answer
4
q
prompt
which of the following is correct:
type
single
option
string temp [] = new string {"j" "a" "z"};
option
string temp [] = { "j " " b" "c"};
option
string temp = {"a", "b", "c"};
option
string temp [] = {"a", "b", "c"};
answer
4
q
prompt
what is the correct declaration of an abstract method that is intended to be
public:
type
single
option
public abstract void add();
option
public abstract void add() {}
option
public abstract add();
option
public virtual add();
answer
1
q
prompt
under what situations do you obtain a default constructor?
type
single
option
when you define any class
option
when the class has no other constructors
option
when you define at least one constructor
answer
2
q
prompt
which of the following can be used to define a constructor for this class, given
the following code:
code
public class test {
...
}
endcode
type
single
option
public void test() {...}
option
public test() {...}
option
public static test() {...}
option
public static void test() {...}
answer
2
q
prompt
which of the following are acceptable to the java compiler:
type
multiple
option
if (2 == 3) system.out.println("hi");
option
if (2 = 3) system.out.println("hi");
option
if (true) system.out.println("hi");
option
if (2 != 3) system.out.println("hi");
option
if (astring.equals("hello")) system.out.println("hi");
answer
1
answer
3
answer
4
answer
5
q
prompt
assuming a method contains code which may raise an exception (but not a
runtimeexception), what is the correct way for a method to indicate that it
expects the
caller to handle that exception:
type
single
option
throw exception
option
throws exception
option
new exception
option
don't need to specify anything
answer
2
q
prompt
what is the result of executing the following code, using the parameters 4 and 0:
code
public void divide(int a, int b) {
try {
int c = a / b;
} catch (exception e) {
system.out.print("exception ");
} finally {
system.out.println("finally");
}
endcode
type
singe
option
prints out: exception finally
option
prints out: finally
option
prints out: exception
option
no output
answer
1
q
prompt
which of the following is a legal return type of a method overloading the
following method:
code

public void add(int a) {...}

endcode
type
single
option
void
option
int
option
can be anything
answer
3
q
prompt
which of the following statements is correct for a method which is overriding the
following method:
code

public void add(int a) {...}

endcode
type
single
option
the overriding method must return void
option
the overriding method must return int
option
the overriding method can return whatever it likes
answer
1
q
prompt
given the following classes defined in separate files, what will be the effect of
compiling and running this class test?
code
class vehicle {
public void drive() {
system.out.println("vehicle: drive");
}
}

class car extends vehicle {


public void drive() {
system.out.println("car: drive");
}
}

public class test {


public static void main (string args []) {
vehicle v;
car c;
v = new vehicle();
c = new car();
v.drive();
c.drive();
v = c;
v.drive();
}
}
endcode
type
single
option
generates a compiler error on the statement v= c;
option
generates runtime error on the statement v= c;
option
prints out:
list
vehicle: drive
car: drive
car: drive
endlist
option
prints out:
list
vehicle: drive
car: drive
vehicle: drive
endlist
answer
3
q
prompt
where in a constructor, can you place a call to a constructor defined in the super
class?
type
single
option
anywhere
option
the first statement in the constructor
option
the last statement in the constructor
option
you can't call super in a constructor
answer
2
q
prompt
which variables can an inner class access from the class which encapsulates it?
type
multiple
option
all static variables
option
all final variables
option
all instance variables
option
only final instance variables
option
only final static variables
answer
1
answer
2
answer
3
q
prompt
what class must an inner class extend:
type
single
option
the top level class
option
the object class
option
any class or interface
option
it must extend an interface
answer
3
q
prompt
in the following code, which is the earliest statement, where the object
originally held in e, may be garbage collected:
code
1. public class test {
2. public static void main (string args []) {
3. employee e = new employee("bob", 48);
4. e.calculatepay();
5. system.out.println(e.printdetails());
6. e = null;
7. e = new employee("denise", 36);
8. e.calculatepay();
9. system.out.println(e.printdetails());
10. }
11. }
endcode
type
single
option
line 10
option
line 11
option
line 7
option
line 8
option
never
answer
3
q
prompt
what is the name of the interface that can be used to define a class that can
execute within its own thread?
type
single
option
runnable
option
run
option
threadable
option
thread
option
executable
answer
1
q
prompt
what is the name of the method used to schedule a thread for execution?
type
single
option
init();
option
start();
option
run();
option
resume();
option
sleep();
answer
2
q
prompt
which methods may cause a thread to stop executing?
type
multiple
option
sleep();
option
stop();
option
yield();
option
wait();
option
notify();
option
notifyall()
option
synchronized()
answer
1
answer
2
answer
3
answer
4
q
prompt
which of the following would create a text field able to display 10 characters
(assuming a fixed size font) displaying the initial string "hello":
:
type
single
option
new textfield("hello", 10);
option
new textfield("hello");
option
new textfield(10);
option
new textfield();
answer
1
q
prompt
which of the following methods are defined on the graphics class:
type
multiple
option
drawline(int, int, int, int)
option
drawimage(image, int, int, imageobserver)
option
drawstring(string, int, int)
option
add(component);
option
setvisible(boolean);
option
setlayout(object);
answer
1
answer
2
answer
3
q
prompt
which of the following layout managers honours the preferred size of a component:
type
multiple
option
cardlayout
option
flowlayout
option
borderlayout
option
gridlayout
answer
2
q
prompt
given the following code what is the effect of a being 5:
code
public class test {
public void add(int a) {
loop: for (int i = 1; i < 3; i++){
for (int j = 1; j < 3; j++) {
if (a == 5) {
break loop;
}
system.out.println(i * j);
}
}
}
}
endcode
type
single
option
generate a runtime error
option
throw an arrayindexoutofboundsexception
option
print the values: 1, 2, 2, 4
option
produces no output
answer
4
q
prompt
what is the effect of issuing a wait() method on an object
type
single
option
if a notify() method has already been sent to that object then it has no effect
option
the object issuing the call to wait() will halt until another object sends a
notify() or notifyall() method
option
an exception will be raised
option
the object issuing the call to wait() will be automatically synchronized with any
other objects using the receiving object.
answer
2
q
prompt
the layout of a container can be altered using which of the following methods:
type
multiple
option
setlayout(alayoutmanager);
option
addlayout(alayoutmanager);
option
layout(alayoutmanager);
option
setlayoutmanager(alayoutmanager);
answer
1
q
prompt
using a flowlayout manager, which is the correct way to add elements to a
container:
type
single
option
add(component);
option
add("center", component);
option
add(x, y, component);
option
set(component);
answer
1
q
prompt
given that a button can generate an actionevent which listener would you expect
to have to implement, in a class which would handle this event?
type
single
option
focuslistener
option
componentlistener
option
windowlistener
option
actionlistener
option
itemlistener
answer
4
q
prompt
which of the following, are valid return types, for listener methods:
type
single
option
boolean
option
the type of event handled
option
void
option
component
answer
3
q
prompt
assuming we have a class which implements the actionlistener interface, which
method should be used to register this with a button?
type
single
option
addlistener(*);
option
addactionlistener(*);
option
addbuttonlistener(*);
option
setlistener(*);
answer
2
q
prompt
in order to cause the paint(graphics) method to execute, which of the following
is the most appropriate method to call:
type
single
option
paint()
option
repaint()
option
paint(graphics)
option
update(graphics)
option
none - you should never cause paint(graphics) to execute
answer
2
q
prompt
which of the following illustrates the correct way to pass a parameter into an
applet:
type
single
option
<applet code=test.class age=33 width=100 height=100>
option
<param name=age value=33>
option
<applet code=test.class name=age value=33 width=100 height=100>
option
<applet test 33>
answer
2
q
prompt
which of the following correctly illustrate how an inputstreamreader can be
created:
type
multiple
option
new inputstreamreader(new fileinputstream("data"));
option
new inputstreamreader(new filereader("data"));
option
new inputstreamreader(new bufferedreader("data"));
option
new inputstreamreader("data");
option
new inputstreamreader(system.in);
answer
1
answer
5
q
prompt
what is the permanent effect on the file system of writing data to a new
filewriter("report"), given the file report already exists?
type
single
option
the data is appended to the file
option
the file is replaced with a new file
option
an exception is raised as the file already exists
option
the data is written to random locations within the file
answer
2
q
prompt
what is the effect of adding the sixth element to a vector created in the
following manner:
code

new vector(5, 10);

endcode
type
single
option
an indexoutofbounds exception is raised.
option
the vector grows in size to a capacity of 10 elements
option
the vector grows in size to a capacity of 15 elements
option
nothing, the vector will have grown when the fifth element was added
answer
3
q
prompt
what is the result of executing the following code when the value of x is 2:
code
switch (x) {
case 1:
system.out.println(1);
case 2:
case 3:
system.out.println(3);
case 4:
system.out.println(4);
}
endcode
type
single
option
nothing is printed out
option
the value 3 is printed out
option
the values 3 and 4 are printed out
option
the values 1, 3 and 4 are printed out
answer
3
q
prompt
consider the following example:
code
class first {
public first (string s) {
system.out.println(s);
}
}
public class second extends first {
public static void main(string args []) {
new second();
}
}
endcode
type
single
what is the result of compiling and running the second class?
option
nothing happens
option
a string is printed to the standard out
option
an instance of the class first is generated
option
an instance of the class second is created
option
an exception is raised at runtime stating that there is no null parameter
constructor in class first.
option
the class second will not compile as there is no null parameter constructor in the
class first
answer
6
q
prompt
what is the result of executing the following fragment of code:
code
boolean flag = false;
if (flag = true) {
system.out.println("true");
} else {
system.out.println("false");
}
endcode
type
single
option
true is printed to standard out
option
false is printed to standard out
option
an exception is raised
option
nothing happens
answer
1
q
prompt
consider the following classes. what is the result of compiling and running this
class?
code
public class test {
public static void test() {
this.print();
}
public static void print() {
system.out.println("test");
}
public static void main(string args []) {
test();
}
}
endcode
type
multiple
option
the string test is printed to the standard out.
option
a runtime exception is raised stating that an object has not been created.
option
nothing is printed to the standard output.
option
an exception is raised stating that the method test cannot be found.
option
an exception is raised stating that the variable this can only be used within an
instance.
option
the class fails to compile stating that the variable this is undefined.
answer
6
q
prompt
examine the following class definition:
code
public class test {
public static void test() {
print();
}
public static void print() {
system.out.println("test");
}
public void print() {
system.out.println("another test");
}
}
endcode
type
single
what is the result of compiling this class:
option
a successful compilation.
option
a warning stating that the class has no main method.
option
an error stating that there is a duplicated method.
option
an error stating that the method test() will call one or other of the print()
methods.
answer
3
q
prompt
what is the result of compiling and executing the following java class:
code
public class threadtest extends thread {
public void run() {
system.out.println("in run");
suspend();
resume();
system.out.println("leaving run");
}

public static void main(string args []) {


(new threadtest()).start();
}
}
endcode
type
single
option
compilation will fail in the method main.
option
compilation will fail in the method run.
option
a warning will be generated for method run.
option
the string "in run" will be printed to standard out.
option
both strings will be printed to standard out.
option
nothing will happen.
answer
4
q
prompt
given the following sequence of java statements, which of the following options
are true:
code
1. stringbuffer sb = new stringbuffer("abc");
2. string s = new string("abc");
3. sb.append("def");
4. s.append("def");
5. sb.insert(1, "zzz");
6. s.concat(sb);
7. s.trim();
endcode
type
multiple
option
the compiler would generate an error for line 1.
option
the compiler would generate an error for line 2.
option
the compiler would generate an error for line 3.
option
the compiler would generate an error for line 4.
option
the compiler would generate an error for line 5.
option
the compiler would generate an error for line 6.
option
the compiler would generate an error for line 7.
answer
4
answer
6
q
prompt
what is the result of executing the following java class:
code
import java.awt.*;

public class frametest extends frame {


public frametest() {
add (new button("first"));
add (new button("second"));
add (new button("third"));
pack();
setvisible(true);
}
public static void main(string args []) {
new frametest();
}
}
endcode
type
single
option
nothing happens.
option
three buttons are displayed across a window.
option
a runtime exception is generated (no layout manager specified).
option
only the "first" button is displayed.
option
only the "second" button is displayed.
option
only the "third" button is displayed.
answer
6
q
prompt
consider the following tags and attributes of tags, which can be used with the
<aaplet> and </applet> tags?
code
1. codebase
2. alt
3. name
4. class
5. javac
6. horizontalspace
7. verticalspace
8. width
9. param
10. jar
endcode
type
multiple
option
line 1, 2, 3
option
line 2, 5, 6, 7
option
line 3, 4, 5
option
line 8, 9, 10
option
line 8, 9
answer
1
answer
5
q
prompt
which of the following is a legal way to construct a randomaccessfile:
type
single
option
randomaccessfile("data", "r");
option
randomaccessfile("r", "data");
option
randomaccessfile("data", "read");
option
randomaccessfile("read", "data");
answer
1
q
prompt
carefully examine the following code, when will the string "hi there" be printed?
code
public class statictest {
static {
system.out.println("hi there");
}
public void print() {
system.out.println("hello");
}
public static void main(string args []) {
statictest st1 = new statictest();
st1.print();
statictest st2 = new statictest();
st2.print();
}
}
endcode
type
single
option
never.
option
each time a new instance is created.
option
once when the class is first loaded into the java virtual machine.
option
only when the static method is called explicitly.
answer
3
q
prompt
what is the result of the following program:
code
public class test {
public static void main (string args []) {
boolean a = false;
if (a = true)
system.out.println("hello");
else
system.out.println("goodbye");
}
}
endcode
type
single
option
program produces no output but terminates correctly.
option
program does not terminate.
option
prints out "hello"
option
prints out "goodbye"
answer
3
q
prompt
examine the following code, it includes an inner class, what is the result:
code
public final class test4 {
class inner {
void test() {
if (test4.this.flag); {
sample();
}
}
}
private boolean flag = true;
public void sample() {
system.out.println("sample");
}
public test4() {
(new inner()).test();
}
public static void main(string args []) {
new test4();
}
}
endcode
type
single
option
prints out "sample"
option
program produces no output but terminates correctly.
option
program does not terminate.
option
the program will not compile
answer
1
q
prompt
carefully examine the following class:
code
public class test5 {
public static void main (string args []) {
/* this is the start of a comment
if (true) {
test5 = new test5();
system.out.println("done the test");
}
/* this is another comment */
system.out.println ("the end");
}
}
endcode
type
single
option
prints out "done the test" and nothing else.
option
program produces no output but terminates correctly.
option
program does not terminate.
option
the program will not compile.
option
the program generates a runtime exception.
option
the program prints out "the end" and nothing else.
option
the program prints out "done the test" and "the end"
answer
6
q
prompt
what is the result of compiling and running the following applet:
code
import java.applet.applet;
import java.awt.*;

public class sample extends applet {


private string text = "hello world";
public void init() {
add(new label(text));
}
public sample (string string) {
text = string;
}
}

it is accessed form the following html page:

<html>
<title>sample applet</title>
<body>
<applet code="sample.class" width=200 height=200></applet>
</body>
</html>
endcode
type
single
option
prints "hello world".
option
generates a runtime error.
option
does nothing.
option
generates a compile time error.
answer
2
q
prompt
what is the effect of compiling and (if possible) running this class:
code
public class calc {
public static void main (string args []) {
int total = 0;
for (int i = 0, j = 10; total > 30; ++i, --j) {
system.out.println(" i = " + i + " : j = " + j);
total += (i + j);
}
system.out.println("total " + total);
}
}
endcode
type
single
option
produce a runtime error
option
produce a compile time error
option
print out "total 0"
option
generate the following as output:
list
i = 0 : j = 10
i = 1 : j = 9
i = 2 : j = 8
total 30
endlist
answer
3
qend

Você também pode gostar