(Quiz) SCJP (Sun Certified Java Program) Declarations and Access Control
QUIZ: SCJP (Sun Java Certification Program) Declarations and Access Control
Questions 1. Which of
these array declarations and instantiations are not legal?
Select all valid Answers:
(a) int []a[] = new int[4][4];
(b) int a[][] = new int[4][4];
(c) int a[][] = new int[][4];
(d) int []a[] = new int[4][];
(e) int[][] a = new int[4][4];
(f) int [ ] [ ] a = new int[4][4];
Answers:1 (c)
The [] notation can be placed both before and after the variable name in an
array declaration. Multidimensional arrays are created by creating arrays that
can contain references to other arrays. The statement new int[4][] will create
an array of length 4, which can contain references to arrays of int values. The
statement new int[4][4] will create the same array, but will also create four
arrays, each containing four int values. References to each of these arrays are
stored in the first array. The statement int [][4] will not work, because the
dimensions must be created from left to right. Extra spaces are not significant.
Questions 2 Which of these
array declarations and initialization are not legal?
Select all valid Answers:
(a) int []i[] = {{1,2}, {1}, {}, {1,2,3}};
(b) int i[]= new int[2]{1, 2};
(c) int i[][] = new int[][]{{1,2,3}, {4,5,6}};
(d) int i[][] = {{1, 2}, new int[2]};
(e) int i[4] = {1, 2, 3, 4};
Answers:2 (b), (e)
The size of the array cannot be specified as in (b), (e). The size of the array
is given implicitly by the initialization code. The sizer of the array is never
given during the declaration of an array reference. The size of an array is
always associated with the array instance, not the array reference.
Questions 3 Given the
following code, which statements can be placed at the indicated position without
causing compilation errors?
public class ThisUsage {
int planets;
static int suns;
public void gaze() {
int i;
// … insert statements here …
}
}
Select all valid Answers:
(a) i = this.planets;
(b) i = this.suns;
(c) this = new ThisUsage();
(d) this.i = 4;
(e) this.suns = planets;
Answers:3 (a), (b), (e)
Non-static methods have an implicit this object reference. The this reference is
not a normal reference variable that can be changed in the way attempted by
statement (c). The this reference can be used to refer to both object and class
members within the current context. However, it cannot be used to refer to local
variables in the way attempted by statement (d).
Questions 4 Given the
following pairs of method declarations, which of these statements ar true?
void fly(int distance) {}
int fly(int time, int speed) {return time*speed}
void fall(int time) {}
int fall(int distance) {return distance}
void glide(int time) {}
void Glide(int time) {}
Select all valid Answers:
(a) The first pair of methods will compile correctly and overload the method
name fly.
(b) The second pair of methods will compile correctly and overload the method
name fall.
(c) The third pair of methods will compile correctly and overload the method
name glide.
(d) The second pair of methods will not compile correctly.
(e) The third pair of methods will not compile correctly.
Answers:4 (a), (d)
The first and third pairs of methods will compile correctly. The second pair of
methods will not compile correctly, since their method signatures do not differ
and the compiler
has therefore no way of diffrentiating between the two methods. Note that return
type and the names of the parameters are not a part of the method signatures.
Both methods in the first pair named fly and therefore overload this method
name. The methods in pair three do not overload the method name glide, since
only one method has the name. The method name Glide is distinct from the method
name glide, as identifiers in Java are case-sensitive.
Questions 5 Given a class
named Book, which of these would be valid definitions of constructors for the
class? [1]
Select all valid Answers:
(a) Book(Book b) {}
(b) Book Book() {}
(c) private final Book() {}
(d) void Book() {}
(e) public static void Book(String args[]) {}
(f) abstract Book() {}
Answers:5 (a)
A constructor does not declare any return type, not even void. A constructor
cannot be final, static or abstract.
Questions 6 Which of these
statements are true?
Select all valid Answers:
(a) All classes must define a constructor.
(b) A constructor can be declared private.
(c) A constructor can declare a return value.
(d) A constructor must initialize all the member variables of a class.
(e) A constructor can access the non-static members of a class.
Answers:6 (b), (e)
A constructor can be declared private, but this means that this constructor can
only be used directly within the class. Constructors need not initialize all the
member variables in a class. A member variable will be assigned a default value
if not explicitly initialized. A constructor is non-static, and as such it can
access directly both the static and non-static members of the class.
Questions 7 What will be the
result of attempting to compile the following program?
Public class MyClass {
Long var;
Public void MyClass(long param) { var = param; } // (1)
Public static void main( String args[] ) {
MyClass a, b;
A = new MyClass(); // (2)
B = new MyClass(5); // (3)
}
}
Select the one right answer:
(a) A compilation error will be encountered at (1), since constructors should
not specify a return value.
(b) A compilation error will be encountered at (2), since the class does not
have a default constructor
(c) A compilation error will be encountered at (3), since the class does not
have a constructor accepting a single argument of type int.
(d) The program will compile correctly.
Answers:7 (c)
A compilation error will be encountered in (3), since the class does not have a
constructor accepting a single argument of type int. The declaration at (1)
declares a method, not a constructor, since it have a return type. The method
happens to have the same name as the class, but that is irrelevant. The class
has an implicit default constructor since the class contains no constructor
declarations. This allows the instantiation at (2) to work.
Questions 8 Given the
following class, which of these are valid ways of referring to the class from
outside of the package net.basemaster?
package net.basemaster;
public class Base {
// …
}
Select all valid Answers:
(a) By simply referring to the class as Base.
(b) By simply referring to the class as basemaster.Base
(c) By simply referring to the class as net.basemaster.Base
(d) By importing net.basemaster.* and referring to the class as Base
(e) By importing the package net.* and referring to the class as basemaster.Base
Answers:8 (c), (d)
A class or interface name can be referred to by using either its fully qualified
name or its simple name. Using the fully qualified name will always work, but in
order to use the simple name it has to be imported. By importing net.basemaster.*
all the type names from the package net.basemaster will be imported and can now
be referred to using simple names. Importing net.* will not import the
subpackage basemaster
Questions 9 Which one of the
following class definitions is a legal definition of a class that cannot be
instantiated?
Select the one right answer:
(a) class Ghost {
abstract void haunt()
}
(b) abstract class Ghost {
void haunt();
}
(c) abstract class Ghost {
void haunt() {};
}
(d) abstract Ghost {
abstract void haunt();
}
(e) static class Ghost {
abstract haunt();
}
Answers:9 (C)
A class is uninstantiable if the class is declared abstract. The declarartion of
an abstract method cannot provide an implementation. The declaration of a
non-abstract method must provide an implemenation. If any method in a class is
declared abstract, then the whole class must be declared abstract. Definition
(d) is not valid, since it omits the class keyword.
Questions 10 Which of these
statements concerning the use of modifiers are true?
Select all valid Answers:
(a) If no accessibility modifier (public, protected and private) is given in a
member declaration of a class, the member is only accessible for classes in the
same package and subclasses of the class.
(b) You cannot specify visibility of local variables. They are always only
accessible within the block in which they ardeclared.
(c) Subclasses of a class must reside in the same package as the class they
extend.
(d) Local variables can be declared static.
(e) Objects themselves don not have visibility, only references to the object.
Answers:10 (b), (e)
You cannot specify visibility of local variables. They are accessible only
within the block in which they are declared. Objects themselves do not have any
visibility, only the references to the object. If no visibility modifier(public,
protected or private) is given in the member declaration of a class, the member
is only accessible to classes in the same package. A class does not have access
to members of a superclass with default accessibility, unless both classes are
in the same package. Inheritance has no consequence with respect to accessing
members wit hdefaul;t accessibility. Local variables acannot be declared static
and cannot be given an accessibility modifier.
Questions 11 Given the
following source code, which one of the lines that are commeneted out may be
reinserted without introducing errors?
abstract class MyClass {
abstract void f();
final void g() {}
// final void h() {} // (1)
protected static int i;
private int j;
}
final class MyOtherClass extends MyClass {
// MyOtherClass(int n) { m = n; } // (2)
public static void main(String args[]) {
MyClass mc = new MyOtherClass();
}
void f() {}
void h() {}
// void k() {i++;} // (3)
// void l() {j++;} // (4)
int m;
}
Select the one right answer:
(a) (1)
(b) (2)
(c) (3)
(d) (4)
Answers:11 (c)
The line “void k() { i++; }” can be reinserted without introducing errors.
Reinserting line (1) will cause the compilation to fail, since MyOtherClass will
try to override a final method. Reinserting line(2) will fail since MyOtherClass
will no longer have a default constructor. The main() method needs a constructor
that takes zero arguments. Reinserting line (3) will work without any problems,
but reinserting line (4) will fail, since the method will try to access a
private member of the superclass
Questions 12 Which of these
statements are true?
Select all valid Answers:
(a) A static method can call other non-static methods in the same class by using
the this keyword.
(b) A class may contain both static and non-static variables and both static and
non-static methods.
(c) Each object of a class has its own instance of each static member variable.
(d) Instance methods may access local variables of static methods.
(e) All methods in a class are implicitly passed a this parameter when called.
Answers:12 (b)
The keyword this can be only used in non-static methods. Only one instance of
each static member variable of a class is created. This instance is shared among
all objects of the class. Local variables are only accessible within the local
scope, regardless of whether the local scope is defined within a static method.
Questions 13 Which of these
statements are true?
Select all valid Answers:
(a) Transient variables will not be saved during serialization.
(b) Constructors can be declared abstract.
(c) The initial state of an array object constructed with the statement int a[]
= new int[10] will depend on whether the variable a[] is a local variable, or a
member variable of a class.
(d) Any subclass of a class with an abstract method must implement a method body
for that method.
(e) Only static methods can access static members.
Answers:13 (a)
The transient keyword signifies that the variables should not be stored when the
object are serialized. Constructors cannot be declared abstract. Elements in an
unitialized array object get the default value corresponding to the type of the
elements. Whether the reference variable pointing to the array object is a local
or a member variable does not matter. Abstract methods from a superclass need
not be implemented by abstract subclass.
Questions 14 Which of the
following are Java
modifiers?
(a) public
(b) private
(c) friendly
(d) transient
(e) vagrant
Answers:14 (a), (b), (d)
The keyword transient is easy to forget as is not frequently used, and it is
considered in the access modifiers because it modifies the behaviour of the
object.
Questions 15 Why might you
define a method as native?
(a) To get to access hardware that Java does not know about.
(b) To define a new data type such as an unsigned integer.
(c) To write optimised code for performance in a language such as C/C++
(d) To overcome the limitation of the private scope of a method
Answers:15 (a), (c)
Questions 16 What will happen
when you attempt to compile and run this code?
public class Mod{
public static void main(String argv[]){
}
public static native void amethod();
}
(a) Error at compilation: native method cannot be static.
(b) Error at compilation native method must return value.
(c) Compilation but error at run time unless you have made code containing
native amethod available.
(d) Compilation and execution without error.
Answers:16 (d)
It would cause a run time error if you had a call to amethod though.
Questions 17 What will happen
when you attempt to compile and run this code?
private class Base{}
public class Vis{
transient int iVal;
public static void main(String elephant[]){
}
}
(a) Compile time error: Base cannot be private.
(b) Compile time error indicating that an integer cannot be transient.
(c) Compile time error transient not a data type.
(d) Compile time error malformed main method.
Answers:17 (a)
A top level (non nested) class cannot be private.
Questions 18 What will happen
when you attempt to compile and run the following code?
public class Hope{
public static void main(String argv[]) {
Hope h = new Hope();
}
protected Hope() {
for(int i =0; i <10; i ++){
System.out.println(i);
}
}
}
(a) Compilation error: Constructors cannot be declared protected.
(b) Run time error: Constructors cannot be declared protected.
(c) Compilation and running with output 0 to 10.
(d) Compilation and running with output 0 to 9.
Answers:18 (d)
Questions 19 What will happen
when you attempt to compile and run the following code with the command line
"hello there"?
public class Arg{
String[] MyArg;
public static void main(String argv[]){
MyArg=argv;
}
public void amethod(){
System.out.println(argv[1]);
}
}
(a) Compile time error.
(b) Compilation and output of "hello".
(c) Compilation and output of "there".
(d) None of the above.
Answers:19 (a)
You will get an error saying something like "Cant make a static reference
to a non static variable". Note that the main method is static. Even if
main was not static the array argv is local to the main method and would thus
not be visible within amethod.
Questions 20 Which of the
following method will not give you any error when placed on line no.3?
Select one correct answer.
1 interface i2
2 {
3 //Here
4 }
(a) static void abc();
(b) abstract void abc();
(c) native void abc();
(d) synchronized void abc();
(e) final void abc();
(f) private void abc();
(g) protected void abc();
Answers:20 (b)
Interface methods can't be native, static, synchronized, final, private, or
protected.
- guru's blog
- Login to post comments
