Navigation |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Topic: Declarations and access control
Question #51 |
|
If you create a TextField with a constructor to set it to occupy 5 columns, what difference will it make if you use it with a proportional font (ie Times Roman) or a fixed pitch typewriter style font (Courier). |
- With a fixed font you will see 5 characters, with a proportional it will depend on the width of the characters
- With a fixed font you will see 5 characters,with a proportional it will cause the field to expand to fit the text
- The columns setting does not affect the number of characters displayed
- Both will show exactly 5 characters
|
Click here to see the answer
|
The Correct Answer is 1: With a fixed font you will see 5 characters, with a proportional it will depend on the width of the characters
|
Click here to see explanation
|
With a proportional font the letter w will occupy more space than the letter i. So if you have all wide characters you may have to scroll to the right to see the entire text of a TextField.
|
Question #52 |
|
Given the following code how could you invoke the Base constructor that will print out the string "base constructor"; |
class Base
{
Base(int i)
{
System.out.println("base constructor");
}
Base(){}
}
public class Sup extends Base
{
public static void main(String argv[])
{
Sup s= new Sup();
//One
}
Sup()
{
//Two
}
public void derived()
{
//Three
}
}
|
- On the line After //One put Base(10);
- On the line After //One put super(10);
- On the line After //Two put super(10);
- On the line After //Three put super(10);
|
Click here to see the answer
|
The Correct Answer is 3: On the line After //Two put super(10);
|
Click here to see explanation
|
Constructors can only be invoked from within constructors.
|
Question #53 |
|
Given the following code what will be output? |
public class Pass
{
static int j=20;
public static void main(String argv[])
{
int i=10;
Pass p = new Pass();
p.amethod(i);
System.out.println(i);
System.out.println(j);
}
public void amethod(int x)
{
x=x*2;
j=j*2;
}
}
|
- Error: amethod parameter does not match variable
- 20 and 40
- 10 and 40
- 10, and 20
|
Click here to see the answer
|
The Correct Answer is 3: 10 and 40
|
Click here to see explanation
|
when a parameter is passed to a method the method receives a copy of the value. The method can modify its value without affecting the original copy. Thus in this example when the value is printed out the method has not changed the value.
|
Question #54 |
|
What code placed after the comment //For loop would result in the population of every element of the array ia[] with a value from variable i.? |
public class Lin
{
public static void main(String argv[])
{
Lin l = new Lin();
l.amethod();
}
public void amethod()
{
int ia[] = new int[4];
//Start For loop
{
ia[i]=i;
System.out.println(ia[i]);
}
}
}
|
- for(int i=0; i < ia.length() -1; i++)
- for (int i=0; i< ia.length(); i++)
- for(int i=1; i < 4; i++)
- for(int i=0; i< ia.length;i++)
|
Click here to see the answer
|
The Correct Answer is 4: for(int i=0; i< ia.length;i++)
|
Click here to see explanation
|
Although you could control the looping with a literal number as with the number 4 used in option 3, it is better practice to use the length property of an array. This provides against bugs that might result if the size of the array changes. This question also checks that you know that arrays starts from zero and not One as option 3 starts from one. Remember that array length is a field and not a function like the String length() method.
|
Question #55 |
|
What will be the result when you try to compile and run the following code? |
private class Base
{
Base()
{
int i = 100;
System.out.println(i);
}
}
public class Pri extends Base
{
static int i = 200;
public static void main(String argv[])
{
Pri p = new Pri();
System.out.println(i);
}
}
|
- Error at compile time
- 200
- 100 followed by 200
- 100
|
Click here to see the answer
|
The Correct Answer is 1: Error at compile time
|
Click here to see explanation
|
This is a slightly sneaky one as it looks like a question about constructors, but it is attempting to test knowledge of the use of the private modifier. A top level class cannot be defined as private. If you didn't notice the modifier private, remember in the exam to be real careful to read every part of the question.
|
Question #56 |
|
What will the following code print out? |
public class Oct
{
public static void main(String argv[])
{
Oct o = new Oct();
o.amethod();
}
public void amethod()
{
int oi= 012;
System.out.println(oi);
}
}
|
- 12
- 012
- 10
- 10.0
|
Click here to see the answer
|
The Correct Answer is 3: 10
|
Click here to see explanation
|
The name of the class might give you a clue with this question, Oct for Octal. Prefixing a number with a zero indicates that it is in Octal format. Thus when printed out it gets converted to base ten. 012 in octal means the first column from the right has a value of 2 and the next along has a value of one times eight. In decimal that adds up to 10.
|
Question #57 |
|
What will happen when you try compiling and running this code? |
public class Ref
{
public static void main(String argv[])
{
Ref r = new Ref();
r.amethod(r);
}
public void amethod(Ref r)
{
int i=99;
multi(r);
System.out.println(i);
}
public void multi(Ref r)
{
r.i = r.i*2;
}
}
|
- Error at compile time
- An output of 99
- An output of 198
- An error at runtime
|
Click here to see the answer
|
The Correct Answer is 1: Error at compile time
|
Click here to see explanation
|
The variable i is created at the level of amethod and will not be available inside the method multi.
|
Question #58 |
|
You need to create a class that will store unique object elements. You do not need to sort these elements but they must be unique.
What interface might be most suitable to meet this need? |
- Set
- List
- Map
- Vector
|
Click here to see the answer
|
The Correct Answer is 1: Set
|
Click here to see explanation
|
The Set interface ensures that its elements are unique, but does not order the elements. In reality you probably wouldn't create your own class using the Set interface. You would be more likely to use one of the JDK classes that use the Set interface such as HashSet or TreeSet.
|
Question #59 |
|
Which of the following will successfully create an instance of the Vector class and add an element? |
- Vector v=new Vector(99);
v[1]=99;
- Vector v=new Vector();
v.addElement(99);
- Vector v=new Vector();
v.add(99);
- Vector v=new Vector(100);
v.addElement("99");
|
Click here to see the answer
|
The Correct Answer is 4: Vector v=new Vector(100);
v.addElement("99");
|
Click here to see explanation
|
A vector can only store objects not primitives. The parameter "99" for the addElement method pases a string object to the Vector. Option 1) creates a vector OK but then uses array syntax to attempt to assign a primitive. Option 2 also creates a vector then uses correct Vector syntax but falls over when the parameter is a primitive instead of an object.
|
Question #60 |
|
You have created a simple Frame and overridden the paint method as follows:
What will be the result when you attempt to compile and run the program? |
public void paint(Graphics g)
{
g.drawString("Dolly",50,10);
}
|
- The string "Dolly" will be displayed at the centre of the frame
- An error at compilation complaining at the signature of the paint method
- The lower part of the word Dolly will be seen at the top of the frame, with the top hidden.
- The string "Dolly" will be shown at the bottom of the frame.
|
Click here to see the answer
|
The Correct Answer is 3: The lower part of the word Dolly will be seen at the top of the frame, with the top hidden.
|
Click here to see explanation
|
The Second parameter to the drawstring method indicates where the baseline of the string will be placed. Thus the 3rd parameter of 10 indicates the Y coordinate to be 10 pixels from the top of the Frame. This will result in just the bottom of the string Dolly showing up or possibly only the descending part of the letter y.
|
|
|
|
|
|
|
|
Today, there have been 1 visitors (2 hits) on this page! |
|
|
|
|
|
|
|