Navigation |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Topic: Declarations and access control
Question #1 |
|
Which of the following statements are true? |
- A constructor may not be declared as private
- A Constructor with no arguments will be the default constructor
- Constructors cannot be overloaded
- A call to a constructor in a parent class can only be made from within a constructor.
|
Click here to see the answer
|
The Correct Answer is 4: A call to a constructor in a parent class can only be made from within a constructor.
|
Click here to see explanation
|
A constructor can be declared as private. Although the default constructor takes no arguments, creating a no args constructor does not make it the default constructor. The default constructor has a special significance in that it is inserted by the java system and executes no code other than to call the zero parameter constructor of the super class.
You could create a zero parameter constructor that executed no code of its own to emulate the default parameter, but simply having no parameters does not make a constructor the default constructor.
Overloading constructors is a very common programming technique.
|
Question #2 |
|
What will happen when you attempt to compile and run the following code? |
public class Mickle
{
public static void main(String argv[])
{
new Mickle();
}
public Mickle()
{
String s[][] = new String[2][2];
System.out.println(s[1][1]);
System.out.println(s[1][2]);
}
}
|
- Compile time error
- Compilation and output of two blanks (with carriage returns)
- Compilation and output of null followed by ArrayIndexOutOfBoundsException
- Compilation and output of blanke followed by ArrayIndexOutOfBoundsException
|
Click here to see the answer
|
The Correct Answer is 3: Compilation and output of null followed by ArrayIndexOutOfBoundsException
|
Click here to see explanation
|
An uninitialised Object that is an array element will be set to null by default. The second call to println because all arrays start from 0, so an array with an element of size 2 has elements 0 and 1 but no element 2.
|
Question #3 |
|
Which of the following statements are true? |
- The abstract modifier may be applied to classes, methods and primitives
- The abstract modifier may be applied to classes but not methods
- A non abstract class that extends an abstract class must implement all the abstract methods of the inherited class
- The abstract modifier may be applied to both classes and methods
|
Click here to see the answer
|
The Correct Answers are
3 & 4: A non abstract class that extends an abstract class must implement all the abstract methods of the inherited class The abstract modifier may be applied to both classes and methods
|
Click here to see explanation
|
No explanation available.
|
Question #4 |
|
What will happen when you attempt to compile and run the following code? |
interface Skeleton
{
public char getSize(int iSize);
}
public class Huskisson implements Skeleton
{
int iSize=99;
public static void main(String argv[])
{
new Huskisson();
}
Huskisson()
{
int iSize=1;
System.out.println(getSize(iSize));
}
public int getSize(int iSize)
{
return iSize;
}
}
|
- Compilation and output of 1
- Compile time error
- Compilation and output of 99
- Compilation but runtime error
|
Click here to see the answer
|
The Correct Answer is 2: Compile time error
|
Click here to see explanation
|
The version of the getSize method in class Huskisson has a different method to the version in the interface it is implementing. In the interface it has a char return type and in Huskisson it has an int return type
|
Question #5 |
|
What will happen when you attempt to compile and run the following code? |
class Vict
{
private Vict()
{
System.out.print("Vict");
}
}
public class BLBeck extends Vict
{
public static void main(String argv[])
{
new BLBeck();
}
BLBeck()
{
System.out.print("BLBeck");
}
}
|
- Compilation and output of "BLBeck"
- Compilation and output of "BLBeckVict"
- Compilation and output of "VictBLBeck"
- Compile time error
|
Click here to see the answer
|
The Correct Answer is 4: Compile time error
|
Click here to see explanation
|
Note that the constructor for Vict is marked as private. A constructor can be marked as private, however if this is done an instance cannot be created from outside of that class via that constructor
|
Question #6 |
|
What will happen when you attempt to compile and run the following code? |
abstract class Town
{
private String sDialingCode;
public String getDialingCode()
{
return sDialingCode;
}
public void setDialingCode(String sDialingCode)
{
this.sDialingCode=sDialingCode;
}
abstract String getCounty();
}
public class Felpersham extends Town
{
public static void main(String argv[])
{
new Felpersham();
}
Felpersham()
{
setDialingCode("01905");
}
}
|
- Compile time error because Felphersham does not implement getCounty method.
- Compilation and no output at runtime
- Compilation but runtime exception
- Compile time error, an abstract class cannot have non abstract methods
- Compile time error, an abstract class has no 'this' instance
|
Click here to see the answer
|
The Correct Answer is 1: Compile time error because Felphersham does not implement getCounty method.
|
Click here to see explanation
|
An abstract class can have non abstract methods, but any class that extends an abstract class must implement (or declare as abstract) the abstract methods in that base class.
|
Question #7 |
|
Which of the following statements are true of a method local inner class? |
- It has access to all members of its enclosing class
- It can only access final variables of its enclosing method
- It can be marked as static
- It can only access final members of its enclosing class
|
Click here to see the answer
|
The Correct Answers are 1 & 2: It has access to all members of its enclosing class It can only access final variables of its enclosing method
|
Click here to see explanation
|
There are no restrictions on on what a method local inner class can access from the enclosing class, though it may only access final members of its enclosing method.
|
Question #8 |
|
Given the following class definition |
public class ShrubHill
{
public void foregate(String sName){}
//Here
}
|
Which of the following methods could be legally placed immediately after the comment //Here |
- public int foregate(String sName){}
- public void foregate(StringBuffer sName){}
- public void foreGate(String sName){}
- private void foregate(String sType){}
|
Click here to see the answer
|
The Correct Answers are 2 & 3: public void foregate(StringBuffer sName){} public void foreGate(String sName){}
|
Click here to see explanation
|
option 1,4 are attempts to redefine a method, the fact that it has an int return type does not contribute to distinguish it from the existing foregate method.
|
Question #9 |
|
Which of the following can legally be inserted into an interface? |
- public static String getPostCode();
- public abstract String getName();
- private String getAverageHousePrice();
- public void setPostCode(String s) { System.out.print(s); }
|
Click here to see the answer
|
The Correct Answer is 2: public abstract String getName();
|
Click here to see explanation
|
An interface may not contain methods marked as static or private. The purpose of interfaces is to set up a contract which other classes actually implement, which contradicts the meaning of a method being either static or private. Methods in interfaces may not have a body, which means option 4 is not legal.
|
Question #10 |
|
What will happen when you attempt to compile and run the following code? |
public class Wolfe
{
public static void main(String argv[])
{
Wolfe w = new Wolfe();
w.Go();
}
public void Go()
{
Account personal = new Account();
personal.rate=7;
Account business = new Account();
business.rate =5;
deduct(business);
System.out.println(business.balance);
System.out.println(personal.rate);
}
public void deduct(Account a)
{
a.balance=200;
}
}
class Account
{
static int rate;
public int balance=100;
}
|
- Compile time error, variable rate in class Account is not initialized
- Output of 200 and 7
- Output of 100 and 5
- Output of 200 and 5
|
Click here to see the answer
|
The Correct Answer is 4: Output of 200 and 5 .
|
Click here to see explanation
|
Variables declared at class level will be initialized to default values so the variable rate in Account will be set to zero when an instance of the class is first created. Because the variable rate is declared as static only one copy will exist no matter how many copies of the class are declared. So setting it to the value 5 for business will also set it for personal. If an object is passed to a method, changes to its variables will be valid wherever the class is visible.
|
|
|
|
|
|
|
|
Today, there have been 7 visitors (9 hits) on this page! |
|
|
|
|
|
|
|