Thursday, May 12, 2011

Java Quiz


True/False questions:

a) You cannot define a method with the same name as the class name and with a return type.
Ans. False.

b) One advantage of static factory method is that like constructors, they are required to create a new object each time they are invoked?
Ans. False. One advantage of static factory method is that unlike constructors, they are not required to create a new object each time they`re invoked

c) Clone method of object class does a shallow copy of this object.
Ans. True

d) Java supports multiple inheritances.
Ans.  No

e) A method that is overridden in the subclass must have the same return type and parameter list.
Ans.  True

f) When a derived class is instantiated, its base class no-argument constructor is called whether or not an explicit call to super( ) is made.
Ans. True

g) Defining formal parameters requires including each parameters type.
 Ans: True. In order for the compiler to check to see if a method call is correct, the compiler  needs to know the types for the parameters being passed. Therefore, all formal parameters (those defined in the method header) must include their type. This is one element that makes Java a Strongly Typed language.

h) While multiple objects of the same class can exist, there is only one version of the class.
Ans: True. Explanation: A class is an abstraction, that is, it exists as a definition, but not as a physical instance. Physical instances are created when an object is instantiated using new. Therefore, there can be many
objects of type String, but only one String class.

Multiple choice questions:

a) In a *.java file you can define classes with the following access modifier(s):

1-  Public classes are only allowed.
2-  Public, private, protected and package level access classes.
3-  Public and package level access classes.
4-  Only one public class in a *.java file is allowed.
5-  One public class and package level access classes.

Ans. 2&3

b) You can add methods to this type. With an implementation, and it will still satisfy the open closed principle.

1-Abstract class
2-Interface
3-Inner class
4-Final class

Ans. 1

c) When will we have static method?

1. When a method doesn't need to access an object state.
2. When a method only needs access to class fields.
3. When it is a factory method.
4. When the method will only be public.
5. None of the above

Ans. 1

d) Consider the following code:
class NewString extends java.lang.String

1. Results in error because java.lang.String is final
2. Results in error because class body is not defined
3. Results in error because the class is not declared public
4. Results in error because String is abstract
5. Compiles successfully

Ans. 1

e) Which one of these primitive types are unsigned?

1. int
2. float
3. char
4. double
5. long

Ans.  3

f) A subclass can directly access

1. public members of a superclass   
2. private members of a superclass   
3. all members of a superclass   
4. none of the members of a superclass 

Ans. 1

g) What is byte code in the context of Java?

1. The type of code generated by a Java compiler
2. The type of code generated by a Java Virtual Machine
3. It is another name for a Java source file
4. It is the code written within the instance methods of a class.
5. It is another name for comments written within a program.

Ans. 2

h) submarine.dive(depth);
What can you say for sure?

1. depth must be an int
2. dive must be a method
3. dive must be the name of an instance field.
4. submarine must be the name of a class
5. submarine must be a method.

Ans. 2