Java

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Java Multiple Choice Questions

1) Which of the following option leads to the portability and security of Java?
a) Bytecode is executed by JVM
b) The applet makes the Java code secure and portable
c) Use of exception handling
d) Dynamic binding between objects

2) Which of the following is not a Java features?


a) Dynamic
b) Architecture Neutral
c) Use of pointers
d) Object-oriented

3) What should be the execution order, if a class has a method, static block, instance
block, and constructor, as shown below?

public class First_C {


public void myMethod()
{
System.out.println("Method");
}

{
System.out.println(" Instance Block");
}

public void First_C()


{
System.out.println("Constructor ");
}
static {
System.out.println("static block");
}
public static void main(String[] args) {
First_C c = new First_C();
c.First_C();
c.myMethod();
}
}

a) Instance block, method, static block, and constructor


b) Method, constructor, instance block, and static block
c) Static block, method, instance block, and constructor
d) Static block, instance block, constructor, and method
4) What will be the output of the following program?

public class MyFirst {


public static void main(String[] args) {
MyFirst obj = new MyFirst(n);
}
static int a = 10;
static int n;
int b = 5;
int c;
public MyFirst(int m) {
System.out.println(a + ", " + b + ", " + c + ", " + n + ", " + m);
}
// Instance Block
{
b = 30;
n = 20;
}
// Static Block
static
{
a = 60;
}
}

a) 10, 5, 0, 20, 0
b) 10, 30, 20
c) 60, 5, 0, 20
d) 60, 30, 0, 20, 0

5) The \u0021 article referred to as a


a) Unicode escape sequence
b) Octal escape
c) Hexadecimal
d) Line feed

6) _____ is used to find and fix bugs in the Java programs.


a) JVM
b) JRE
c) JDK
d) JDB

7) Which of the following is a valid declaration of a char?


a) char ch = '\utea';
b) char ca = 'tea';
c) char cr = \u0223;
d) char cc = '\itea';
8) What is the return type of the hashCode() method in the Object class?
a) Object
b) int
c) long
d) void

9) Which of the following is a valid long literal?


a) ABH8097
b) L990023
c) 904423
d) 0xnf029L

10) What does the expression float a = 35 / 0 return?


Advertisement
a) 0
b) Not a Number
c) Infinity
d) Run time exception

11) Evaluate the following Java expression, if x=3, y=5, and z=10:
++z + y - y + z + x++
a) 24
b) 23
c) 20
d) 25

12) What will be the output of the following program?


public class Test {
public static void main(String[] args) {
int count = 1;
while (count <= 15) {
System.out.println(count % 2 == 1 ? "***" : "+++++");
++count;
} // end while
} // end main
}

a) 15 times ***
b) 15 times +++++
c) 8 times *** and 7 times +++++
d) Both will print only once
13) Which of the following tool is used to generate API documentation in HTML format
from doc comments in source code?
a) javap tool
b) javaw command
c) Javadoc tool
d) javah command

14) Which of the following creates a List of 3 visible items and multiple selections
abled?
a) new List(false, 3)
b) new List(3, true)
c) new List(true, 3)
d) new List(3, false)

15) Which of the following for loop declaration is not valid?


a) for ( int i = 99; i >= 0; i / 9 )
b) for ( int i = 7; i <= 77; i += 7 )
c) for ( int i = 20; i >= 2; - -i )
d) for ( int i = 2; i <= 20; i = 2* i )

16) Which method of the Class.class is used to determine the name of a class
represented by the class object as a String?
Advertisement
a) getClass()
b) intern()
c) getName()
d) toString()

17) In which process, a local variable has the same name as one of the instance
variables?
a) Serialization
b) Variable Shadowing
c) Abstraction
d) Multi-threading

18) Which of the following is true about the anonymous inner class?
a) It has only methods
b) Objects can't be created
c) It has a fixed class name
d) It has no class name

19) Which package contains the Random class?


Advertisement
a) java.util package
b) java.lang package
c) java.awt package
d) java.io package
20) What do you mean by nameless objects?
a) An object created by using the new keyword.
b) An object of a superclass created in the subclass.
c) An object without having any name but having a reference.
d) An object that has no reference.

21) An interface with no fields or methods is known as a ______.


a) Runnable Interface
b) Marker Interface
c) Abstract Interface
d) CharSequence Interface

22) Which of the following is an immediate subclass of the Panel class?


a) Applet class
b) Window class
c) Frame class
d) Dialog class

23) Which option is false about the final keyword?


a) A final method cannot be overridden in its subclasses.
b) A final class cannot be extended.
c) A final class cannot extend other classes.
d) A final method can be inherited.

24) Which of these classes are the direct subclasses of the Throwable class?
a) RuntimeException and Error class
b) Exception and VirtualMachineError class
c) Error and Exception class
d) IOException and VirtualMachineError class

25) What do you mean by chained exceptions in Java?


a) Exceptions occurred by the VirtualMachineError
b) An exception caused by other exceptions
c) Exceptions occur in chains with discarding the debugging information
d) None of the above

26) In which memory a String is stored, when we create a string using new operator?
a) Stack
b) String memory
c) Heap memory
d) Random storage space
27) What is the use of the intern() method?
a) It returns the existing string from memory
b) It creates a new string in the database
c) It modifies the existing string in the database
d) None of the above

28) Which of the following is a marker interface?


a) Runnable interface
b) Remote interface
c) Readable interface
d) Result interface

29) Which of the following is a reserved keyword in Java?


a) object
b) strictfp
c) main
d) system

30) Which keyword is used for accessing the features of a package?


a) package
b) import
c) extends
d) export

31) In java, jar stands for_____.


a) Java Archive Runner
b) Java Application Resource
c) Java Application Runner
d) None of the above

32) What will be the output of the following program?

public class Test2 {


public static void main(String[] args) {
StringBuuer s1 = new StringBuuer("Complete");
s1.setCharAt(1,'i');
s1.setCharAt(7,'d');
System.out.println(s1);
}
}

a) Complete
b) Iomplede
c) Cimpletd
d) Coipletd
33) Which of the following is false?
a) The rt.jar stands for the runtime jar
b) It is an optional jar file
c) It contains all the compiled class files
d) All the classes available in rt.jar is known to the JVM

34) What is the use of \w in regex?


a) Used for a whitespace character
b) Used for a non-whitespace character
c) Used for a word character
d) Used for a non-word character

35) Which of the given methods are of Object class?


a) notify(), wait( long msecs ), and synchronized()
b) wait( long msecs ), interrupt(), and notifyAll()
c) notify(), notifyAll(), and wait()
d) sleep( long msecs ), wait(), and notify()

36) Given that Student is a class, how many reference variables and objects are created
by the following code?

Student studentName, studentId;


studentName = new Student();
Student stud_class = new Student();

a) Three reference variables and two objects are created.


b) Two reference variables and two objects are created.
c) One reference variable and two objects are created.
d) Three reference variables and three objects are created.

37) Which of the following is a valid syntax to synchronize the HashMap?


a) Map m = hashMap.synchronizeMap();
b) HashMap map =hashMap.synchronizeMap();
c) Map m1 = Collections.synchronizedMap(hashMap);
d) Map m2 = Collection.synchronizeMap(hashMap);
38) Given,

ArrayList list = new ArrayList();


What is the initial quantity of the ArrayList list?

a) 5
b) 10
c) 0
d) 100
39) Which of the following is a mutable class in java?
Advertisement
a) java.lang.String
b) java.lang.Byte
c) java.lang.Short
d) java.lang.StringBuilder

40) What will be the output of the following program?


abstract class MyFirstClass
{
abstract num (int a, int b) { }
}
a) No error
b) Method is not defined properly
c) Constructor is not defined properly
d) Extra parentheses

41) What is meant by the classes and objects that dependents on each other?
a) Tight Coupling
b) Cohesion
c) Loose Coupling
d) None of the above

42) Given,

int values[ ] = {1,2,3,4,5,6,7,8,9,10};


for(int i=0;i< Y; ++i)
System.out.println(values[i]);

Find the value of value[i]?


a) 10
b) 11
c) 15
d) None of the above

43) Which of the following code segment would execute the stored procedure
"getPassword()" located in a database server?
a) CallableStatement cs = connection.prepareCall("{call.getPassword()}");
cs.executeQuery();
b) CallabledStatement callable = conn.prepareCall("{call getPassword()}");
callable.executeUpdate();
c) CallableStatement cab = con.prepareCall("{call getPassword()}");
cab.executeQuery();
d) Callablestatement cstate = connect.prepareCall("{call getpassword()}");
cstate.executeQuery();
44) How many threads can be executed at a time?
a) Only one thread
b) Multiple threads
c) Only main (main() method) thread
d) Two threads

45) If three threads trying to share a single object at the same time, which condition will
arise in this scenario?
a) Time-Lapse
b) Critical situation
c) Race condition
d) Recursion

46) If a thread goes to sleep


a) It releases all the locks it has.
b) It does not release any locks.
c) It releases half of its locks.
d) It releases all of its lock except one.

47) Which of the following modifiers can be used for a variable so that it can be
accessed by any thread or a part of a program?
a) global
b) transient
c) volatile
d) default

48) What is the result of the following program?

public static synchronized void main(String[] args) throws


InterruptedException {
Thread f = new Thread();
f.start();
System.out.print("A");
f.wait(1000);
System.out.print("B");
}

a) It prints A and B with a 1000 seconds delay between them


b) It only prints A and exits
c) It only prints B and exits
d) A will be printed, and then an exception is thrown.

49) In character stream I/O, a single read/write operation performs _____.


a) Two bytes read/write at a time.
b) Eight bytes read/write at a time.
c) One byte read/write at a time.
d) Five bytes read/ write at a time.
50) What is the default encoding for an OutputStreamWriter?
a) UTF-8
b) Default encoding of the host platform
c) UTF-12
d) None of the above

You might also like