Core Java Test Questions
Core Java Test Questions
A
B
C
D
5. What will happen when you attempt to compile and run this code?
class Base{
public final void amethod(){
System.out.println("amethod");
}
}
public class Fin extends Base{
public static void main(String argv[]){
Base b = new Base();
b.amethod();
}
}
1) Compile time error indicating that a class with any final methods must be declared
final itself
2) Compile time error indicating that you cannot inherit from a class with final methods
3) Run time error indicating that Base is not defined as final
4) Success in compilation and output of "amethod" at run time.
6. You want to find out the value of the last element of an array. You write the following
code. What will happen when you compile and run it.?
public class MyAr{
public static void main(String argv[]){
int[] i = new int[5];
System.out.println(i[5]);
}
}
1) An error at compile time
2) An error at run time
3) The value 0 will be output
4) The string "null" will be output
7. What best describes the appearance of an application with the following code?
import java.awt.*;
public class FlowAp extends Frame{
public static void main(String argv[]){
FlowAp fa=new FlowAp();
fa.setSize(400,300);
fa.setVisible(true);
}
FlowAp(){
add(new Button("One"));
add(new Button("Two"));
add(new Button("Three"));
add(new Button("Four"));
}//End of constructor
}//End of Application
1) A Frame with buttons marked One to Four placed on each edge.
2) A Frame with buutons marked One to four running from the top to bottom
3) A Frame with one large button marked Four in the Centre
4) An Error at run time indicating you have not set a LayoutManager
8. What will be printed out if you attempt to compile and run the following code ?
int i=1;
switch (i) {
case 0:
System.out.println("zero");
break;
case 1:
System.out.println("one");
case 2:
System.out.println("two");
default:
System.out.println("default");
}
1)one
2) one, default
3) one, two, default
4) default
9. What will happen if you attempt to compile and run the following code?
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;
}
}
1) Compile and run without error
2) Compile time Exception
3) Runtime Exception
10. What will be output by the following code?
public class MyFor{
public static void main(String argv[]){
int i;
int j;
outer:
for (i=1;i <3;i++)
inner:
for(j=1; j<3; j++) {
if (j==2)
continue outer;
System.out.println("Value for i=" + i + " Value for j=" +j);
}
}
}
1) Value for i=1 value for j=1
2) Value for i=2 value for j=1
3) Value for i=2 value for j=2
4) Value for i=3 value for j=1
11. Which two are valid constructors for Thread?
1. Thread(Runnable r, String name)
2. Thread()
3. Thread(int priority)
4. Thread(Runnable r, ThreadGroup g)
5. Thread(Runnable r, int priority)
System.out.println("In test");
System.out.println(this);
int temp = this.total;
if (temp > 5) {
System.out.println(temp);
}
}
}
A. The class will not compile
B. The compiler reports and error at line 2
C. The compiler reports an error at line 9
D. The value 10 is one of the elements printed to the standard output
E. The class compiles but generates a runtime error
23. Which of the following is correct:
A. String temp [] = new String {"j" "a" "z"};
B. String temp [] = { "j " " b" "c"};
C. String temp = {"a", "b", "c"};
D. String temp [] = {"a", "b", "c"};
24. Under what situations do you obtain a default constructor?
A. When you define any class
B. When the class has no other constructors
C. When you define at least one constructor
25. Given the following code:
public class Test {
}
Which of the following can be used to define a constructor for this class:
A. public void Test() {}
B. public Test() {}
C. public static Test() {}
D. public static void Test() {}
29. What is the value of "d" after this line of code has been executed?
double d = Math.round ( 2.5 + Math.random() );
A.
B.
C.
D.
2
3
4
2.5
30. Which variables can an inner class access from the class which encapsulates it?
A. All static variables
B. All final variables
C. All instance variables
D. Only final instance variables
E. Only final static variables
31 What will be the output of the program?
try
{
int x = 0;
int y = 5 / x;
}
catch (Exception e)
{
System.out.println("Exception");
}
catch (ArithmeticException ae)
{
System.out.println(" Arithmetic Exception");
}
System.out.println("finished");
A. finished
B. Exception
C. Compilation fails.
D. Arithmetic Exception
32 In the following code, which is the earliest statement, where the object originally held
in
e, may be garbage collected:
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. }
A. Line 10
B. Line 11
C. Line 7
D. Line 8
E. Never
35
package testpackage;
public class Test{
public static void main(String[] args) {
try {
int a= (int)(Math.random()*5);
if(a<=2.5)
System.out.println(""a=""+a);
else
throw new Exception(""a>2.5"");
} catch (Exception e){
System.err.println(e.getMessage() );
System.err.println("Value of a="+a);
}}}
Runtime exception
a=2
Which of the above can be used within the <APPLET> and </APPLET> tags?
A. line 1, 2, 3
B. line 2, 5, 6, 7
C. line 3, 4, 5
D. line 8, 9, 10
E. line 8, 9
39. Carefully examine the following 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();
}
}
When will the string "Hi there" be printed?
A. Never.
B. Each time a new instance is created.
C. Once when the class is first loaded into the Java virtual machine.
D. Only when the static method is called explicitly.