EXAM
1.
public class MyClass {
public static void main(String
arguments[]) {
amethod(arguments);
}
public void amethod(String[]
arguments) {
System.out.println(arguments);
System.out.println(arguments[1]);
}
}
2
What will be printed out if you attempt to compile and run the following code?int i=9;
switch (i) {
default:
System.out.println("default");
case 0:
System.out.println("zero");
break;
case 1:
System.out.println("one");
case 2:
System.out.println("two");
}
1) default3.
Which of the following lines of code will compile without error1)
int i=0;
if(i) {
System.out.println("Hello");
}
2)
boolean b=true;
boolean b2=true;
if(b==b2) {
System.out.println("So true");
}
3)
int i=1;
int j=2;
if(i==1|| j==2)
System.out.println("OK");
4)
int i=1;
int j=2;
if(i==1 &| j==2)
System.out.println("OK");
4.
What tags are mandatory when creating HTML to display an applet1) name, height, width
2) code, name
3) codebase, height, width
4) code, height, width
5.
What will happen if you attempt to compile and run the following code?1) Compile and run without error
2) Compile time Exception
3) Runtime Exception
class Base {}
class Sub extends Base {}
class Sub2 extends Base {}
public class CEx{
public static void main(String argv[]){
Base b=new Base();
Sub s=(Sub) b;
}
}
6.
You are browsing the Java HTML documentation for information on the1) Define your own Listener interface according to the event to be tracked
2) Use the search facility in the HTML documentation for the listener needed
3) Move up the hierarchy in the HTML documentation to locate methods in base
classes
4) Subclass awt.event with the appropriate Listener method
7.
Given the following declarationsString s1=new String("Hello")
String s2=new String("there");
String s3=new String();
Which of the following are legal operations?
1) s3=s1 + s2;
2) s3=s1-s2;
3) s3=s1 & s2
4) s3=s1 && s2
8.
public class MyClass1 {
public static void main(String argv[]){ }
/*Modifier at XX */ class MyInner {}
}What modifiers would be legal at XX in the above code?
1) public
2) private
3) static
4) friend
9.
What will happen when you attempt to compile and run the following code?.
1) It will compile and the run method will print out the increasing value of
i.
2) It will compile and calling start will print out the increasing value of i.
3) The code will cause an error at compile time.
4) Compilation will cause an error because while cannot take a parameter of
true.
class Background implements Runnable{
int i=0;
public int run(){
while(true){
i++;
System.out.println("i="+i);
} //End while
}//End run
}//End class
10.
What will be the result when you attempt to compile and run the following code?.
public class Conv{
public static void main(String argv[]){
Conv c=new Conv();
String s=new String("ello");
c.amethod(s);
}
public void amethod(String s){
char c='H';
c+=s;
System.out.println(c);
}
}
1) Compilation and output the string "Hello"
11.
You have created a simple Frame and overridden the paint method as followspublic void paint(Graphics g){
g.drawString("Dolly",50,10);
}
What will be the result when you attempt to compile and run the program?
1) The string "Dolly" will be displayed at the centre of the frame
2) An error at compilation complaining at the signature of the paint method
3) The lower part of the word Dolly will be seen at the top of the form, with
the top hidden.
4) The string "Dolly" will be shown at the bottom of the form
12.
13.
14.
Answer
1.
Objective 3.1
1) Can't make static reference to void amethod.
Because main is defined as static you need to create an instance of the class
in order to call any non-static methods. Thus a typical way to do this would be.
MyClass m=new MyClass();
m.amethod();
Answer 2 is an attempt to confuse because the convention is for a main method to be in the form
String argv[]
That argv is just a convention and any acceptable identifier for a string array can be used. Answers 3 and 4 are just nonsense.
2.
Objective 4.1)
2) default, zero
Although it is normally placed last the default default statement does not have to be the last item as you fall through the case bock Because there is no case label found matching the expression the default label is executed and the code continues to fall through until it encounters a break.
3.
Objective 4.2,2,3
Example 1 will not compile because if must always test a boolean. This can
catch out C/C++ programmers who expect the test to be for either 0 or not 0.
4.
Objective Unknown4) code, height, width
5.
Objective 5.8 (sort of)3) Runtime Exception
Without the cast to sub you would get a compile time error. The cast tells
the compiler that you really mean to do this and the actual type of b does not
get resolved until runtime. Casting down the object hierarchy as the compiler
cannot be sure what has been implemented in descendent classes. Casting up is
not a problem because sub classes will have the features of the base classes.
This can feel counter intuitive if you are aware that with primitives casting is
allowed for widening operations (ie byte to int).
6.
Objective 1.1)3) Move up the hierarchy in the HTML documentation to locate methods in base
classes
The documentation created by JavaDoc is based on tags placed into the
sourcecode. The convention for documentation is that methods and fields of
ancestors are not duplicated in sub classes. So if you are looking for something
and it does not appear to be there, you move up the class hierarchy to find it.
7.
Objective 2.21) s3=s1 + s2;
Java does not allow operator overloading as in C++, but for the sake of convenience the + operator is overridden for strings.
8.
Objective 3.7)1,2,3
public, private, static are all legal access modifiers for this inner class.
9.
Objective 7.2)3) The code will cause an error at compile time
The error is caused because run should have a void not an int return type.
Any class that is implements an interface must create a method to match all of the methods in the interface. The Runnable interface has one method called run that has a void return type.The sun compiler gives the error
Method redefined with different return type: int run() was defined as void
run();
10.
Objective 2.24) Compile time error
The only operator overloading offered by java is the + sign for the String class. A char is a 16 bit integer and cannot be concatenated to a string with the + operator.
11.
Objective 9.5)3) The lower part of the word Dolly will be seen at the top of the form
The Second parameter to the drawstring method indicates where the baseline of the string will be placed. Thus the 3rd parameter of 10 indicates the Y coordinate to be 10 pixels from the top of the Frame. This will result in just the bottom of the string Dolly showing up or possibly only the descending part of the letter y.
12.