Java Multiple Choice Questions and Answers
Java Multiple Choice Questions and Answers
Answers
1) The default value of a static integer variable of a class in Java is,
A) a
A) b
A) a
A) b
(a) static (b) const (c) final (d) abstract (e) none of the above.
A) c
A) c
A) c
A) c
(a) A finally block is executed before the catch block but after the try block.
(b) A finally block is executed, only after the catch block is executed.
(c) A finally block is executed whether an exception is thrown or not.
(d) A finally block is executed, only if an exception occurs.
(e) None of the above.
A) c
Java Multiple Choice Questions And
Answers For Experienced
11) Among these expressions, which is(are) of type String?
A) d
After the above piece of code is executed, what are the colors of r1 and
r2 (in this order)?
(a) Color.blue
Color.red
(b) Color.blue
Color.blue
(c) Color.red
Color.red
(d) Color.red
Color.blue
(e) None of the above.
A) c
13) What is the type and value of the following expression? (Notice the
integer division)
-4 + 1/2 + 2*-3 + 5.0
(a) int -5 (b) double -4.5
(c) int -4
(d) double -5.0 (e) None of the above.
A) d
A) c
A) d
16) Which of the following variable declaration would NOT compile in a
java program?
(a) int var; (b) int VAR; (c) int var1; (d) int var_1; (e) int 1_var;.
A) e
The method setValue assigns the value of i to the instance field value. What
could you write for the implementation of setValue?
A) d
A) d
19) A constructor
(a) Must have the same name as the class it is declared within.
(b) Is used to create objects.
(c) May be declared private
(d) Both (A) and (B) above
(e) (a), (b) and (c) above.
A) e
20) Consider,
A) a
A) a
Java Multiple Choice Questions 22) What is garbage collection in the
context of Java?
(a) The operating system periodically deletes all the java files available on the
system.
(b) Any package imported in a program and not used is automatically deleted.
(c) When all references to an object are gone, the memory used by the object
is automatically reclaimed.
(d) The JVM checks the output of any Java program and deletes anything that
doesn’t make sense.
(e) Janitors working for Sun Micro Systems are required to throw away any
Microsoft documentation found in the employees’ offices.
A) c
Java Multiple Choice Questions 23) You read the following statement in a
Java program that compiles and executes.
submarine.dive(depth);
A) b
Java Multiple Choice Questions 24) The java run time system
automatically calls this method while garbage collection.
A) b
Java Multiple Choice Questions 25) The correct order of the declarations
in a Java program is,
(a) Package declaration, import statement, class declaration
(b) Import statement, package declaration, class declaration
(c) Import statement, class declaration, package declaration
(d) Class declaration, import statement, package declaration
(e) Class declaration, package declaration, import statement.
A) a
A) d
(a) a subclass of the same package (b) a non-subclass of the same package
(c) a non-subclass of different package (d) a subclass of different package
(e) the same class.
A) c
Java Multiple Choice Questions 28) What is the output of the following
code:
class eq
{
public static void main(String args[])
{
String s1 = “Hello”;
String s2 = new String(s1);
System.out.println(s1==s2);
}
}
A) b
Java Multiple Choice Questions 29) All exception types are subclasses of
the built-in class
A) d
A) b
(a) Java is a fully object oriented language with strong support for proper
software engineering techniques
(b) In java it is not easy to write C-like so called procedural programs
(c) In java language objects have to be manipulated
(d) In java language error processing is built into the language
(e) Java is not a language for internet programming.
A) d
A) c
A) c
Java Multiple Choice Questions 34) Identify, from among the following,
the incorrect variable name(s).
A) c
Java Multiple Choice Questions 35) Use the following declaration and
initialization to evaluate the Java expressions
int a = 2, b = 3, c = 4, d = 5;
float k = 4.3f;
System.out.println( – -b * a + c *d – -);
A) b
Java Multiple Choice Questions 36) Use the following declaration and
initialization to evaluate the Java expressions
int a = 2, b = 3, c = 4, d = 5;
float k = 4.3f;
System.out.println(a++);
A) b
Java Multiple Choice Questions 37) Use the following declaration and
initialization to evaluate the Java expressions
int a = 2, b = 3, c = 4, d = 5;
float k = 4.3f;
A) e
Java Multiple Choice Questions 38) Use the following declaration and
initialization to evaluate the Java expressions
int a = 2, b = 3, c = 4, d = 5;
float k = 4.3f;
System.out.println (c=c++);
A) b
Java Multiple Choice Questions 39) Consider the following Java program :
class IfStatement{
public static void main(String args[])
{
int a=2, b=3;
if (a==3)
if (b==3)
System.out.println(“===============”);
else
System.out.println(“#################”);
System.out.println(“&&&&&&&&&&&”);
}
}
(a) ===============
(b) #################
&&&&&&&&&
(c) &&&&&&&&&&&
(d) ===============
#################
&&&&&&&&&&
(e) ################.
A) c
A) d
class argumentPassing
{
public static void main(String args[])
{
test ob = new test();
int a = 15, b = 20;
A) c
42) What would the argument passing method be which is used by the above
Program – III?
class prob1{
int puzzel(int n){
int result;
if (n==1)
return 1;
result = puzzel(n-1) * n;
return result;
}
}
class prob2{
}
}
A) d
44) The blank space in the following sentence has to be correctly filled :
A) d
45) Java compiler javac translates Java source code into ………………………
A) b
46) ……………….. are used to document a program and improve its readability.
(a) System cells (b) Keywords (c) Comments (d) Control structures (e) Blocks.
A) c
(a) EBCDIC (b) Unicode (c) ASCII (d) Binary (e) BCD.
A) b
(a) Throw (b) Run (c) Exit (d) Catch (e) Error.
A) d
49) An abstract data type typically comprises a …………… and a set of ………………
respectively.
A) d
(a) Binding (b) Transferring (c) Hiding (d) Coupling (e) extending.
A) e
(a) Interpreter (b) Destructor (c) Constructor (d) Object (e) Compiler.
A) c
I. Packages don’t provide a mechanism to partition all class names into more
manageable chunks.
II. Packages provide a visibility control mechanism.
III. One of the important properties of a package is that all classes defined
inside a package is accessible by code outside that package.
IV. The .class files for classes declared to be part of a package can be stored in
multiple directories.
Which of them is correct?
(a) Only (I) above (b) Only (II) above
(c) Only (III) above (d) Only (IV) above
(e) All (I), (II), (III) and (IV) above are wrong.
A) b
(a) Both (I) and (II) above (b) Both (III) and (IV) above
(c) Both (I) and (III) above (d) Both (II) and (IV) above
(e) Only (III) above.
A) e
55) Identify, from among the following, the incorrect descriptions related to
Java :
(a) Java Virtual Machine translates byte code into its own system’s machine
language and runs the resulting machine code
(b) The arithmetic operations *, /, %, + and – have the same level of
precedence
(c) Comments do not cause any action to be performed during the program
execution
(d) All variables must be given a type when they are declared
(e) Java variable names are case-sensitive.
A) b
(a) Both (I) and (III) above (b) Both (II) and (IV) above
(c) Both (I) and (II) above (d) (III) and (IV) above
(e) All (I), (II), (III) and (IV) above.
A) d
(a) Both (I) and (II) above (b) (I), (II), (III) and (IV) above
(c) (I), (II) and (III) above (d) (II) and (III) above
(e) All (I), (II), (III), (IV) and (V) above.
A) c
58) For what values respectively of the variables gender and age would the
Java expression gender == 1 && age >= 65 become true?
A) c
A) b
A) d
A) b
A) b
A) d
5) Which of the following does not belong: If a class inherits from some
other class, it should
A) c
A) a
A) a
8) Polymorphism
A) e
A) d
A) a
(a) The operating system periodically deletes all of the java files available on
the system.
(b) Any package imported in a program and not used is automatically deleted.
(c) When all references to an object are gone, the memory used by the object
is automatically reclaimed.
(d) The JVM checks the output of any Java program and deletes anything that
doesn’t make sense.
(e) When all references to an object are gone the memory used by the object
is not reclaimed.
A) c
A) c
A) b
Abstract class xy
{
abstract sum (int x, int y) { }
}
A) c
15) Which of these field declarations are legal within the body of an
interface?
(a) Private final static int answer = 42 (b) public static int answer=42
(c) final static answer =42 (d) int answer
(e) No error.
A) b
16) A package is a collection of
A) d
17) A method within a class is only accessible by classes that are defined
within the same package as the class of the method. Which one of the
following is used to enforce such restriction?
A) d
18) Basic Java language functions are stored in which of the following
java package?
(a) java.lang (b) java.io (c) java.net (d) java.util (e) java.awt
A) a
(a) List (b) Queue (c) Math (d) Stack (e) Process.
A) c
The class “Math” contains methods for performing basic numeric operations
such as the elementary exponential, logarithm, square root, and trigonometric
functions.
A) b
A) a
Core Java Multiple Choice Questions # 22) File class is included in which
package?
A) a
String s1 = ” yes” ;
String s2 = ” yes ” ;
String s3 = new String ( s1);
A) a
Core Java Multiple Choice Questions # 24) URL stands for
A) d
Core Java Multiple Choice Questions # 25) What is the sequence of major
events in the life of an applet?
(a) init, start, stop, destroy (b) start, init , stop , destroy
(c) init, start , destroy, stop (d) init, start, destroy
(e) destroy, start, init, stop.
A) a
Core Java Multiple Choice Questions # 26) Which of the following is true in
regard to applet execution?
(a) Applets loaded from the same computer where they are executing have the
same restrictions as applets loaded from the network.
(b) Applets loaded and executing locally have none of the restrictions faced by
applets that get loaded from the network.
(c) Applets loaded and executing locally have some restrictions faced by
applets that get loaded from the network.
(d) Applets cant react to user input and change dynamically
(e) Applets can be run independently.
A) b
Core Java Multiple Choice Questions # 27) What is the return type of the
method getID() defined in AWTEvent class
(a) Int (b) long (c) Object (d) Component (e) float.
A) a
Core Java Multiple Choice Questions # 28) Which of the following events will
cause a thread to die?
A) d
Core Java Multiple Choice Questions # 29) What will be the result of the
expression 13 & 25?
A) c
Core Java Multiple Choice Questions # 30) Which of the following statements
are true regarding the finalize( ) method?
A) d
(a)
void method1
(b)
void method2()
(c)
void method3(void)
(d)
method4()
(e)
methods(void).
A) b
Core Java Multiple Choice Questions # 32) Given a class named Book, which
one of these is a valid constructor declaration for the class?
(a)
Book(Book b) { }
(b)
Book Book() { }
(c)
private final Book() { }
(d)
void Book() { }
(e)
abstract Book() { }.
A) a
Core Java Multiple Choice Questions # 33) What will be the result of
attempting to compile the following program?
A) c
Core Java Multiple Choice Questions # 34) Given the following class, which of
these is valid way of referring to the class from outside of the package
net.basemaster?
package net.basemaster;
public class Base {
// . . .
}
Select the correct answer.
(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 simply referring to the class as net.Base
(e)
By importing with net.* and referring to the class as basemaster.Base.
A) c
Core Java Multiple Choice Questions # 35) Which one of the following class
definitions is a valid definition of a class that cannot be instantiated?
(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();
}
A) c
A) e
37) Given the following definition of a class, which fields are accessible from
outside the package com.corporation.project?
package com.corporation.project;
public class MyClass
{
int i;
public int j;
protected int k;
private int l;
}
Select the correct answer.
(a)
Field i is accessible in all classes in other packages
(b)
Field j is accessible in all classes in other packages
(c)
Field k is accessible in all classes in other packages
(d)
Field l is accessible in all classes in other packages
(e)
Field l is accessible in subclasses only in other packages.
A) b
38) How restrictive is the default accessibility compared to public, protected,
and private accessibility?
(a)
Less restrictive than public
(b)
More restrictive than public, but less restrictive than protected
(c)
More restrictive than protected, but less restrictive than private
(d)
More restrictive than private
(e)
Less restrictive than protected from within a package, and more restrictive
than protected from outside a package.
A) c
(a)
Private members are always accessible from within the same package
(b)
Private members can only be accessed by code from within the class of the
member
(c)
A member with default accessibility can be accessed by any subclass of the
class in which it is defined
(d)
Private members cannot be accessed at all
(e)
Package/default accessibility for a member can be declared using the keyword
default.
A) b
(a)
If no accessibility modifier (public, protected, and private) is specified for a
member declaration, the member is only accessible for classes in the package
of its class and subclasses of its class anywhere
(b)
You cannot specify accessibility of local variables. They are only accessible
within the block in which they are declared
(c)
Subclasses of a class must reside in the same package as the class they extend
(d)
Local variables can be declared static
(e)
None of the above.
A) b