Java Quiz
The following code is for Questions 81 and 82
//Contents of File AnInterface.java
public interface AnInterface
{
public void methodOne() throws Exception;
}
//Contents of File AnInterfaceImpl.java
public class AnInterfaceImpl implements AnInterface
{
public void methodOne()
{
System.out.println("I will never throw an exception");
}
}
Question 81.
Read the code below carefully.
public class ATest
{
public static void main(String args[])
{
AnInterface ai = new AnInterfaceImpl();
ai.methodOne();
}
}
Attempting to compile and run the above code
Question 82.
Read the code below carefully.
public class ATest
{
public static void main(String args[])
{
AnInterfaceImpl ai = new AnInterfaceImpl();
ai.methodOne();
}
}
Attempting to compile and run the above code
Question 83
Read the code below carefully
//Contents of Constants.java
public class Constants
{
public static final String greetingString="Hello";
}
//Contents of SomeClass.java
public class SomeClass
{
public static void main(String[] args)
{
System.out.println("Greeting String is "+Constants.greetingString);
}
}
The Constants.java file is edited and the
greetingString is changed to "Hello World!". The file after editing is
shown below.
public class Constants
{
public static final String greetingString="Hello World!";
}
On Compilation of Constants.java and running
of the application gives. (Note : The client class SomeClass is not compiled)
Question 84.
Read the code below carefully
import java.util.*;
public class AllFinals
{
final Vector v;
public AllFinals()
{
}
}
The above code will
Question 85.
import java.util.*;
public class AllFinals
{
{
final Vector v;
v=new Vector();
}
public AllFinals()
{
}
public void someMethod()
{
System.out.println(v.isEmpty());
}
}
An attempt to compile and call the someMethod of the above class at
runtime will cause
Question 86.
//Contents of File AllFinals.java
import java.util.*;
public class AllFinals
{
final Vector v;
public AllFinals()
{
v=new Vector();
}
public AllFinals(int
i)
{
}
public void someMethod()
{
System.out.println(v.isEmpty());
}
}
An attempt to compile the above code will cause
someMethod
is called on an instance that was created through the constructor with the integer argument.
Question 87.
public class A
{
final StringBuffer sb = new StringBuffer("I
am final");
public A()
{
}
public StringBuffer getSb()
{
return this.sb;
}
public static void main(String[] args)
{
A a = new A();
StringBuffer localSB
= a.getSb();
localSB.append("....");
localSB = new StringBuffer();
System.out.println(localSB.toString());
}
}
Attempting to compile and run the above application will yield
Question 88.
public class A
{
StringBuffer sb = new StringBuffer("I
am final");
public A()
{
}
public final StringBuffer getSb()
{
return this.sb;
}
public static void main(String[] args)
{
A a = new A();
StringBuffer localSB
= a.getSb();
localSB.append("....");
localSB = new StringBuffer();
System.out.println(localSB.toString());
}
}
Attempting to compile and run the above application will yeild
Question 89.
public class A
{
StringBuffer sb = new StringBuffer("I
am final");
public A()
{
}
public StringBuffer getSb()
{
return this.sb;
}
public static void main(String[] args)
{
A a = new A();
final StringBuffer localSB
= a.getSb();
localSB.append("....");
localSB = new StringBuffer();
System.out.println(localSB.toString());
}
}
Attempting to compile and run the above application will yeild
Question 90.
//contents of file A.java
public class A
{
A()
{
class B
{
static
{
System.out.println("I am getting loaded");
}
}
}
A(int i)
{
System.out.println("No B this time");
}
}
class C
{
public static void main(String[] args)
{
A a = new A(1);
}
}
Attemping to compile and execute the above Code
Question 91.
Read the following code excerpt carefully.
//Contents of file A.java
public class A
Which of the following are true
{
A()
{
class B
{
{
System.out.println("I am in no-arg constructor");
}
}
}
A(int i)
{
class B
{
{
System.out.println("I am in the arg constructor");
}
}
new B();
}
}
class C
{
public static void main(String[] args)
{
A a = new A(1);
}
}
A.java
cannot be compiled. Duplicate defination of inner class B.
A.java
compiles without any error. An attempt to run C
as an application will cause runtime linkage error. Duplicate definations of inner class B are found
"I am in the arg constructor"
to be printed on the console