Navigation |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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();
|
- s3=s1 + s2;
- s3=s1-s2;
- s3=s1 & s2;
- s3=s1 && s2;
|
Click here to see the answer
|
The Correct Answer is 1: s3=s1 + s2;
|
Click here to see explanation
|
Java does not allow operator overloading as in C++, but for the sake of convenience the + operator is overridden for strings.
|
Question #42 |
|
What is the result of the following operation? |
System.out.println(4 | 3);
|
- 6
- 0
- 1
- 7
|
Click here to see the answer
|
The Correct Answer is 4: 7
|
Click here to see explanation
|
The | is known as the Or operator, you could think of it as the either/or operator. Turning the numbers into binary gives
4=100
3=011
For each position, if either number contains a 1 the result will contain a result in that position. As every position contains a 1 the result will be
111
Which is decimal 7.
|
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 {}
}
|
- public
- private
- static
- friend
|
Click here to see the answer
|
The Correct Answer is 1: public 2: private 3: static
|
Click here to see explanation
|
public, private, static are all legal access modifiers for this inner class.
|
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());
}
}
}
}
|
- Compile time error
- Output of first0, second0, first0, second1
- Output of first0, first1, second0, second1
- Runtime error
|
Click here to see the answer
|
The Correct Answer is 3: Output of first0, first1, second0, second1
|
Click here to see explanation
|
Note that this code overrides and calls the start method. If you wished to get the output mixed you would need to override the run method but call the start method.
|
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
|
- It will compile and the run method will print out the increasing value of i.
- It will compile and calling start will print out the increasing value of i.
- The code will cause an error at compile time.
- Compilation will cause an error because while cannot take a parameter of true.
|
Click here to see the answer
|
The Correct Answer is 3: The code will cause an error at compile time.
|
Click here to see explanation
|
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();
|
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);
}
}
}
|
- Compilation error due to malformed parameter to go method
- Compilation error, class Turing has no start method
- Compilation and output of 0 followed by 1
- Compilation but runtime error
|
Click here to see the answer
|
The Correct Answer is 3: Compilation and output of 0 followed by 1
|
Click here to see explanation
|
The creation of an anonymous class as a parameter to go is fairly strange as you would expect it to override a method in its parent class (Turing). You don't have to though. The fact that class Turing extends Thread means the anonymous instance that is passed to go has a start method which then calls the run method.
|
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);
}
}
|
- Compilation and output the string "Hello"
- Compilation and output the string "ello"
- Compilation and output the string elloH
- Compile time error
|
Click here to see the answer
|
The Correct Answer is 4: Compile time error
|
Click here to see explanation
|
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.
|
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");
}
}
}
|
- if(s==s2)
- if(s.equals(s2)
- if(s.equalsIgnoreCase(s2))
- if(s.noCaseMatch(s2))
|
Click here to see the answer
|
The Correct Answer is 3: if(s.equalsIgnoreCase(s2))
|
Click here to see explanation
|
String comparison is case sensitive so using the equals string method will not return a match. Using the==operator just compares where memory address of the references and noCaseMatch was just something I made up to give me a fourth slightly plausible option.
|
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);
}
}
|
- s.setBackground(Color.pink);
- s.setColor(PINK);
- s.Background(pink);
- s.color=Color.pink
|
Click here to see the answer
|
The Correct Answer is 1: s.setBackground(Color.pink);
|
Click here to see explanation
|
For speakers of the more British spelt English note that there is no letter u in Color. Also the constants for colors are in lower case.
|
Question #50 |
|
How can you change the current working directory using an instance of the File class called FileName? |
- FileName.chdir("DirName")
- FileName.cd("DirName")
- FileName.cwd("DirName")
- The File class does not support directly changing the current directory.
|
Click here to see the answer
|
The Correct Answer is 4: The File class does not support directly changing the current directory.
|
Click here to see explanation
|
This seems rather surprising to me, as changing the current directory is a very common requirement. You may be able to get around this limitation by creating a new instance of the File class passing the new directory to the constructor as the path name.
|
|
|
|
|
|
|
|
Today, there have been 2 visitors (4 hits) on this page! |
|
|
|
|
|
|
|