0% found this document useful (0 votes)
62 views3 pages

Quiz 3

Uploaded by

i
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views3 pages

Quiz 3

Uploaded by

i
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Software Development Quiz 3

1. Which one of these declarations is a valid method declaration?


Select the one correct answer.
(a) void method1 { /* ... */ }
(b) void method2() { /* ... */ }
(c) void method3(void) { /* ... */ }
(d) method4() { /* ... */ }
(e) method5(void) { /* ... */ }

2. Given the following pairs of method declarations, which statements are 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 the two correct answers.
(a) The first pair of methods will compile, and overload the method name fly.
(b) The second pair of methods will compile, and overload the method name
fall.
(c) The third pair of methods will compile, and overload the method name glide.
(d) The second pair of methods will not compile.
(e) The third pair of methods will not compile.

3. Which statements are true?


Select the two correct answers.
(a) A class must define a constructor.
(b) A constructor can be declared private.
(c) A constructor can return a value.
(d) A constructor must initialize all fields when a class is instantiated.
(e) A constructor can access the non-static members of a class.

-1-
4. What will be printed when the following program is run?
public class ParameterPass {
public static void main(String[] args) {
int i = 0;
addTwo(i++);
[Link](i);
}

static void addTwo(int i) {


i += 2;
}
}
Select the one correct answer.
(a) 0
(b) 1
(c) 2
(d) 3

5. Which of the following are reserved keywords?


Select the three correct answers.
(a) public
(b) static
(c) void
(d) main
(e) String
(f) args

-2-
Answer

1. (b)
Only (b) is a valid method declaration. Methods must specify a return type or must be declared
void. This makes (d) and (e) invalid. Methods must specify a list of zero or more comma-separated
parameters enclosed by parentheses, ( ). The keyword void cannot be used to specify an empty
parameter list. This makes (a) and (c) invalid.

2. (a) and (d)


The first and the third pairs of methods will compile. The second pair of methods will not compile,
since their method signatures do not differ. The compiler has no way of differentiating between
the two methods. Note that the return type and the names of the parameters are not a part of the
method signatures. Both methods in the first pair are named fly and, therefore, overload this
method name. The methods in the last pair do not overload the method name glide, since only one
method has that name. The method named Glide is distinct from the method named glide, as
identifiers are case-sensitive in Java.

3. (b) and (e)


A constructor can be declared private, but this means that this constructor can only be used within
the class. Constructors need not initialize all the fields when a class is instanstiated. A field will be
assigned a default value if not explicitly initialized. A constructor is non-static and, as such, it can
directly access both the static and non-static members of the class.

4. (b)
Evaluation of the actual parameter i++ yields 0, and increments i to 1 in the process. The value 0 is
copied into the formal parameter i of the method addTwo() during method invocation. However,
the formal parameter is local to the method, and changing its value does not affect the value in the
actual parameter. The value of the variable i in the main() method remains 1.

5. (a), (b), and (c)


Neither main, String, nor args are reserved keywords, but they are legal identifiers. In the
declaration public static void main(String[] args), the identifier main denotes the method that is
the entry point of a program. In all other contexts, the identifier main has no predefined meaning.

-3-

You might also like