SCJP Quiz
  Page #3
 
Topic: Declarations and access control
Question #21

What will happen when you compile and run the following code?
public class MyClass
{
    static int i;
    public static void main(String argv[])
    {
        System.out.println(i);
    }
}
  1. Error Variable i may not have been initialized
  2. null
  3. 1
  4. 0
Click here to see the answer
Click here to see explanation

Question #22

What will happen if you try to compile and run the following code?
public class Q 
{
    public static void main(String argv[])
    {
        int anar[]=new int[]{1,2,3};
        System.out.println(anar[1]);
    }
}
  1. 1
  2. Error anar is referenced before it is initialized
  3. 2
  4. Error: size of array must be defined
Click here to see the answer
Click here to see explanation

Question #23

What will happen if you try to compile and run the following code?
public class Q 
{
    public static void main(String argv[])
    {
        int anar[]=new int[5];
        System.out.println(anar[0]);
    }
}
  1. Error: anar is referenced before it is initialized
  2. null
  3. 0
  4. 5
Click here to see the answer
Click here to see explanation

Question #24

What will be the result of attempting to compile and run the following code?
abstract class MineBase 
{
    abstract void amethod();
    static int i;
}
public class Mine extends MineBase 
{
    public static void main(String argv[])
    {
        int[] ar=new int[5];
        for(i=0;i < ar.length;i++)
        System.out.println(ar[i]);
    }
}
  1. A sequence of 5 0's will be printed
  2. Error: ar is used before it is initialized
  3. Error Mine must be declared abstract
  4. IndexOutOfBoundes Error
Click here to see the answer
Click here to see explanation

Question #25

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
Click here to see the answer
Click here to see explanation

Question #26

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. default
  2. default, zero
  3. Error: default clause not defined
  4. No output displayed
Click here to see the answer
Click here to see explanation

Question #27

Which of the following lines of code will compile without error?
  1. 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");
Click here to see the answer
Click here to see explanation

Question #28

What will be output if you try to compile and run the following code, but there is no file called Hello.txt in the current directory?
import java.io.*;
public class Mine 
{
    public static void main(String argv[])
    {
        Mine m=new Mine();
        System.out.println(m.amethod());
    }
    public int amethod() 
    {
        try 
        {
            FileInputStream dis=new FileInputStream("Hello.txt");
        }
        catch (FileNotFoundException fne) 
        {
            System.out.println("No such file found");
            return -1;
        }
        catch(IOException ioe) 
        {
        } 
        finally
        {
            System.out.println("Doing finally");
        }
        return 0;
    }
}
  1. No such file found
  2. No such file found ,-1
  3. No such file found, Doing finally, -1
  4. 0
Click here to see the answer
Click here to see explanation

Question #29

Which of the following statements are true?
  1. Methods cannot be overridden to be more private.
  2. Static methods cannot be overloaded.
  3. Private methods cannot be overloaded.
  4. An overloaded method cannot throw exceptions not checked in the base class.
Click here to see the answer
Click here to see explanation

Question #30

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
Click here to see the answer
Click here to see explanation

 
  Today, there have been 1 visitors (3 hits) on this page!  
 
This website was created for free with Own-Free-Website.com. Would you also like to have your own website?
Sign up for free