Navigation |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Topic: Declarations and access control
Question #11 |
|
Which of the following statements are true? |
- An array may contain only primitives not objects
- An array may contain primitives or objects
- The elements of an array may themselves be arrays
- Arrays have a size method that returns the number of elements
|
Click here to see the answer
|
The Correct Answers are 2 & 3: An array may contain primitives or objects The elements of an array may themselves be arrays.
|
Click here to see explanation
|
A count of the elements in an array is contained in the length field.
|
Question #12 |
|
Given the following code, which of the options if inserted on the line after the comment //here will allow the code to compile without error? |
class Wchapel
{
String sDescription = "Aldgate";
public String getDescription()
{
return "Mile End";
}
public void output(int i )
{
System.out.println(i);
}
}
interface Liz
{
}
public class Wfowl extends Wchapel implements Liz
{
private int i = 99;
public static void main(String argv[])
{
Wfowl wf = new Wfowl();
wf.go();
}
public void go()
{
//here
}
}
|
- super().output(100);
- new Wfowl().output(i);
- class test implements Liz{}
- System.out.println(sDescription);
- new Wfowl();
- getDescription();
|
Click here to see the answer
|
The Correct Answer is 2,3,4,5 & 6
|
Click here to see explanation
|
No explanation available.
|
Question #13 |
|
Given the following code, which of the options if inserted on the line after the comment //here will allow the code to compile without error? |
public class Kirving
{
private final int i = 10;
public static void main(String argv[])
{
Kirving ki = new Kirving();
ki.go(3);
}
public void go(int z)
{
//here
}
}
|
- int i = z;
- class test{ int j = i; }
- int j = i;
- i++;
|
Click here to see the answer
|
The Correct Answer is 1,2 & 3
|
Click here to see explanation
|
Option 4 is not valid because the variable i is declared as final, meaning its value, once assigned cannot be changed. The ++ operation attempts to change its value.
|
Question #14 |
|
Which of the following lines will compile without warning or error? |
- float f=1.3;
- char c="a";
- byte b=257;
- boolean b=null;
- int i=10;
|
Click here to see the answer
|
The Correct Answer is 5: int i=10;
|
Click here to see explanation
|
1) float f=1.3; Will not compile because the default type of a number with a floating point component is a double. This would compile with a cast as in float f=(float) 1.3
2) char c="a"; Will not compile because a char (16 bit unsigned integer) must be defined with single quotes. This would compile if it were in the form char c='a';
3) byte b=257; Will not compile because a byte is eight bits. Take of one bit for the sign component you can define numbers between -128 to +127
4) A boolean value can either be true or false, null is not allowed.
|
Question #15 |
|
What will happen if you try to compile and run the following code? |
public class MyClass
{
public static void main(String arguments[])
{
amethod(arguments);
}
public void amethod(String[] arguments)
{
System.out.println(arguments);
System.out.println(arguments[1]);
}
}
|
- Error: Can't make static reference to void amethod.
- Error: method main not correct
- Error: array must include parameter
- amethod must be declared with String
|
Click here to see the answer
|
The Correct Answer is 1: Can't make static reference to void amethod.
|
Click here to see explanation
|
Because main is defined as static you need to create an instance of the class in order to call any non-static methods. Thus a typical way to do this would be.
MyClass m=new MyClass();
m.amethod();
Answer 2 is an attempt to confuse because the convention is for a main method to be in the form
String argv[]
That argv is just a convention and any acceptable identifier for a string array can be used. Answers 3 and 4 are just nonsense
|
Question #16 |
|
Which of the following will compile without error? |
import java.awt.*;
package Mypackage;
class Myclass {}
package MyPackage;
import java.awt.*;
class MyClass{}
/*This is a comment */
package MyPackage;
import java.awt.*;
class MyClass{}
|
Click here to see the answer
|
Answer 2 and 3 will compile without error.
|
Click here to see explanation
|
1 will not compile because any package declaration must come before any other code. Comments may appear anywhere.
|
Question #17 |
|
A byte can be of what size? |
- -128 to 127
- (-2 power 8 )-1 to 2 power 8
- -255 to 256
- Depends on the particular implementation of the Java Virtual machine
|
Click here to see the answer
|
The Correct Answer is 1: -128 to 127
|
Click here to see explanation
|
A byte is a signed 8 bit integer.
|
Question #18 |
|
What will be printed out if this code is run with the following command line? |
java myprog good morning
public class myprog
{
public static void main(String argv[])
{
System.out.println(argv[2]);
}
}
|
- myprog
- good
- morning
- Exception: java.lang.ArrayIndexOutOfBoundsException
|
Click here to see the answer
|
The Correct Answer is 4: Exception: java.lang.ArrayIndexOutOfBoundsException
|
Click here to see explanation
|
Unlike C/C++ java does not start the parameter count with the program name. It does however start from zero. So in this case zero starts with good, morning would be 1 and there is no parameter 2 so an exception is raised.
|
Question #20 |
|
Which of the following are legal identifiers? |
- 2variable
- variable2
- _whatavariable
- _3_
- $anothervar
- #myvar
|
Click here to see the answer
|
The Correct Answers are 2, 3, 4 & 5
|
Click here to see explanation
|
An identifier can begin with a letter (most common) or a dollar sign($) or an underscore(_). An identifier cannot start with anything else such as a number, a hash, # or a dash -. An identifier cannot have a dash in its body, but it may have an underscore _. Choice 4) _3_ looks strange but it is an acceptable, if unwise form for an identifier.
|
|
|
|
|
|
|
|
Today, there have been 6 visitors (8 hits) on this page! |
|
|
|
|
|
|
|