SCJP Quiz
  Page #5
 
Topic: Declarations and access control
Question #41

Given the following declarations, Which of the following are legal operations?
String s1=new String("Hello");
String s2=new String("there");
String s3=new String();
  1. s3=s1 + s2;
  2. s3=s1-s2;
  3. s3=s1 & s2;
  4. s3=s1 && s2;
Click here to see the answer
Click here to see explanation

Question #42

What is the result of the following operation?
System.out.println(4 | 3);
  1. 6
  2. 0
  3. 1
  4. 7
Click here to see the answer
Click here to see explanation

Question #43

What modifiers would be legal at XX in the above code?
public class MyClass1 
{
    public static void main(String argv[]){ }
    /*Modifier at XX */ class MyInner {}
}
  1. public
  2. private
  3. static
  4. friend
Click here to see the answer
Click here to see explanation

Question #44

What will happen when you attempt to compile and run the following code?
public class Holt extends Thread
{
    private String sThreadName; 
    public static void main(String argv[])
    {
        Holt h = new Holt();
        h.go(); 
    }
    Holt(){}

    Holt(String s)
    {
        sThreadName = s;
    }
    public String getThreadName()
    {
        return sThreadName;
    }

    public void go()
    {
        Holt first = new Holt("first");
        first.start();
        Holt second = new Holt("second");
        second.start();
    }

    public void start()
    {
        for(int i = 0; i < 2; i ++)
        {
            System.out.println(getThreadName() +i);
            try
            {
                Thread.sleep(100);
            } 
            catch(InterruptedException e)
            {
                System.out.println(e.getMessage());
            }
        }
    }
}
  1. Compile time error
  2. Output of first0, second0, first0, second1
  3. Output of first0, first1, second0, second1
  4. Runtime error
Click here to see the answer
Click here to see explanation

Question #45

What will happen when you attempt to compile and run the following code?
class Background implements Runnable
{
    int i=0;
    public int run()
    {
        while(true)
        {
            i++;
           System.out.println("i="+i);
        } //End while
        return 1;
    }//End run
}//End class
  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.
Click here to see the answer
Click here to see explanation

Question #46

Which of the following statements about this code are true?
public class Morecombe
{
    public static void main(String argv[])
    {
        Morecombe m = new Morecombe();
        m.go(new Turing(){});
    }
    public void go(Turing t)
    {
        t.start();
    }
}
class Turing extends Thread
{
    public void run()
    {
        for(int i =0; i < 2; i++)
        {
           System.out.println(i);
        }
    }
}
  1. Compilation error due to malformed parameter to go method
  2. Compilation error, class Turing has no start method
  3. Compilation and output of 0 followed by 1
  4. Compilation but runtime error
Click here to see the answer
Click here to see explanation

Question #47

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"
  2. Compilation and output the string "ello"
  3. Compilation and output the string elloH
  4. Compile time error
Click here to see the answer
Click here to see explanation

Question #48

Given the following code, what test would you need to put in place of the comment line? //place test here to result in an output of the string Equal
public class EqTest
{
    public static void main(String argv[])
    {
        EqTest e=new EqTest();
    }

    EqTest()
    {
        String s="Java";
        String s2="java";
        //place test here 
        {
            System.out.println("Equal");
        }
        else
        {
            System.out.println("Not equal");
        }
    }
}
  1. if(s==s2)
  2. if(s.equals(s2)
  3. if(s.equalsIgnoreCase(s2))
  4. if(s.noCaseMatch(s2))
Click here to see the answer
Click here to see explanation

Question #49

Given the following code how could you set the frame surface color to pink
import java.awt.*;
public class SetF extends Frame
{
    public static void main(String argv[])
    {
        SetF s=new SetF();
        s.setSize(300,200);
        s.setVisible(true);
    }
}
  1. s.setBackground(Color.pink);
  2. s.setColor(PINK);
  3. s.Background(pink);
  4. s.color=Color.pink
Click here to see the answer
Click here to see explanation

Question #50

How can you change the current working directory using an instance of the File class called FileName?
  1. FileName.chdir("DirName")
  2. FileName.cd("DirName")
  3. FileName.cwd("DirName")
  4. The File class does not support directly changing the current directory.
Click here to see the answer
Click here to see explanation

 
  Today, there have been 2 visitors (4 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