Java MCQ Set 1
1. Which of these events is generated when a button is pressed?
a) ActionEvent
b) KeyEvent
c) WindowEvent
d) AdjustmentEvent
Answer
Answer: a [Reason:] Action event is generated when a button is pressed, a list item is double-clicked or a menu item is selected.
2. What is an event in delegation event model used by Java programming language?
a) An event is an object that describes a state change in a source
b) An event is an object that describes a state change in processing
c) An event is an object that describes any change by the user and system
d) An event is a class used for defining object, to create events
Answer
Answer: a [Reason:] An event is an object that describes a state change in a source.
3. Which of these methods can be used to obtain the command name for invoking ActionEvent object?
a) getCommand()
b) getActionCommand()
c) getActionEvent()
d) getActionEventCommand()
Answer
Answer: b [Reason:] None.
4. Which of these are integer constants defined in ActionEvent class?
a) ALT_MASK
b) CTRL_MASK
c) SHIFT_MASK
d) All of the mentioned
Answer
Answer: d [Reason:] Action event defines 4 integer constants ALT_MASK, CTRL_MASK, SHIFT_MASK and ACTION_PERFORMED
5. Which of these methods can be used to know which key is pressed?
a) getKey()
b) getModifier()
c) getActionKey()
d) getActionEvent()
Answer
Answer: b [Reason:] The getModifiers() methods returns a value that indicates which modifiers keys (ALT, CTRL, META, SHIFT) were pressed when the event was generated.
6. Which of these events is generated by scroll bar?
a) ActionEvent
b) KeyEvent
c) WindowEvent
d) AdjustmentEvent
Answer
Answer: d [Reason:] None.
7. Which of these methods can be used to determine the type of adjustment event?
a) getType()
b) getEventType()
c) getAdjustmentType()
d) getEventObjectType()
Answer
Answer: c [Reason:] None.
8. Which of these methods can be used to know the degree of adjustment made by the user?
a) getValue()
b) getAdjustmentType()
c) getAdjustmentValue()
d) getAdjustmentAmount()
Answer
Answer: a [Reason:] The amount of the adjustment can be obtained from the getvalue() method, it returns an integer value corresponding to the amount of adjustment made.
9. Which of these events will be notified if scroll bar is manipulated?
a) ActionEvent
b) ComponentEvent
c) AdjustmentEvent
d) WindowEvent
Answer
Answer: c [Reason:] AdjustmentEvent is generated when a scroll bar is manipulated.
10. Which of these constant value will change when the button at the end of scroll bar was clicked to increase its value?
a) BLOCK_DECREMENT
b) BLOCK_INCREMENT
c) UNIT_DECREMENT
d) UNIT_INCREMENT
Answer
Answer: d [Reason:] UNIT_INCREMENT VALUE will change when the button at the end of scroll bar was clicked to increase its value.
Java MCQ Set 2
1. Which of these functions is called to display the output of an applet?
a) display()
b) paint()
c) displayApplet()
d) PrintApplet()
Answer
Answer: b [Reason:] Whenever the applet requires to redraw its output, it is done by using method paint().
2. Which of these methods can be used to output a sting in an applet?
a) display()
b) print()
c) drawString()
d) transient()
Answer
Answer: c [Reason:] drawString() method is defined in Graphics class, it is used to output a string in an applet.
3. What does AWT stands for?
a) All Window Tools
b) All Writing Tools
c) Abstract Window Toolkit
d) Abstract Writing Toolkit
Answer
Answer: c [Reason:] AWT stands for Abstract Window Toolkit, it is used by applets to interact with the user.
4. Which of these methods is a part of Abstract Window Toolkit (AWT) ?
a) display()
b) paint()
c) drawString()
d) transient()
Answer
Answer: b [Reason:] paint() is an abstract method defined in AWT.
5. Which of these modifiers can be used for a variable so that it can be accessed from any thread or parts of a program?
a) transient
b) volatile
c) global
d) No modifier is needed
Answer
Answer: b [Reason:] The volatile modifier tells the compiler that the variable modified by volatile can be changed unexpectedly by other part of the program. Specially used in situations involving multithreading.
6. Which of these operators can be used to get run time information about an object?
a) getInfo
b) Info
c) instanceof
d) getinfoof
Answer
Answer: c [Reason:] None.
7. What is the Message is displayed in the applet made by this program?
-
import java.awt.*;
-
import java.applet.*;
-
public class myapplet extends Applet
-
{
-
public void paint(Graphics g)
-
{
-
g.drawString("A Simple Applet", 20, 20);
-
}
-
}
a) A Simple Applet
b) A Simple Applet 20 20
c) Compilation Error
d) Runtime Error
Answer
Answer: a [Reason:] None.
Output:
A Simple Applet
(Output comes in a new java application)
8. What is the length of the application box made by this program?
-
import java.awt.*;
-
import java.applet.*;
-
public class myapplet extends Applet
-
{
-
public void paint(Graphics g)
-
{
-
g.drawString("A Simple Applet", 20, 20);
-
}
-
}
a) 20
b) 50
c) 100
d) System dependent
Answer
Answer: a [Reason:] the code in pain() method – g.drawString(“A Simple Applet”,20,20); draws a applet box of length 20 and width 20.
9. What is the length of the application box made by this program?
-
import java.awt.*;
-
import java.applet.*;
-
public class myapplet extends Applet
-
{
-
Graphic g;
-
g.drawString("A Simple Applet", 20, 20);
-
}
a) 20
b) Default value
c) Compilation Error
d) Runtime Error
Answer
Answer: c [Reason:] To implement the method drawString we need first need to define abstract method of AWT that is paint() method. Without paint() method we can not define and use drawString or any Graphic class methods.
10. What is the output of this program?
-
import java.io.*;
-
class Chararrayinput
-
{
-
public static void main(String[] args)
-
{
-
String obj = "abcdefgh";
-
int length = obj.length();
-
char c[] = new char[length];
-
obj.getChars(0, length, c, 0);
-
CharArrayReader input1 = new CharArrayReader(c);
-
CharArrayReader input2 = new CharArrayReader(c, 1, 4);
-
int i;
-
int j;
-
try
-
{
-
while((i = input1.read()) == (j = input2.read()))
-
{
-
System.out.print((char)i);
-
}
-
}
-
catch (IOException e)
-
{
-
e.printStackTrace();
-
}
-
}
-
}
a) abc
b) abcd
c) abcde
d) none of the mentioned
Answer
Answer: d [Reason:] No output is printed. CharArrayReader object input1 contains string “abcdefgh” whereas object input2 contains string “bcde”, when while((i=input1.read())==(j=input2.read())) is executed the starting character of each object is compared since they are unequal control comes out of loop and nothing is printed on the screen.
Output:
$ javac Chararrayinput.java
$ java Chararrayinput
Java MCQ Set 3
1. Which of these have highest precedence?
a) ()
b) ++
c) *
d) >>
Answer
Answer: a [Reason:] Order of precedence is (highest to lowest) a -> b -> c -> d.
2. What should be expression1 evaluate to in using ternary operator as in this line?
expression1 ? expression2 : expression3
a) Integer
b) Floating – point numbers
c) Boolean
d) None of the mentioned
Answer
Answer:c [Reason:] The controlling condition of ternary operator must evaluate to boolean.
3. What is the value stored in x in following lines of code?
int x, y, z; x = 0; y = 1; x = y = z = 8;
a) 0
b) 1
c) 9
d) 8
Answer
Answer: d [Reason:] None.
4. What is the order of precedence (highest to lowest) of following operators?
1. & 2. ^ 3. ?:
a) 1 -> 2 -> 3
b) 2 -> 1 -> 3
c) 3 -> 2 -> 1
d) 2 -> 3 -> 1
Answer
Answer: a [Reason:] None.
5. Which of these statements are incorrect?
a) Equal to operator has least precedence
b) Brackets () have highest precedence
c) Division operator, /, has higher precedence than multiplication operator
d) Addition operator, +, and subtraction operator have equal precedence
Answer
Answer: c [Reason:] Division operator, /, has equal precedence as of multiplication operator. In expression involving multiplication and division evaluation of expression will begin from right side when no brackets are used.
6. What is the output of this program?
-
class operators
-
{
-
public static void main(String args[])
-
{
-
int var1 = 5;
-
int var2 = 6;
-
int var3;
-
var3 = ++ var2 * var1 / var2 + var2;
-
System.out.print(var3);
-
}
-
}
a) 10
b) 11
c) 12
d) 56
Answer
Answer: c [Reason:] Operator ++ has the highest precedence than / , * and +. var2 is incremented to 7 and then used in expression, var3 = 7 * 5 / 7 + 7, gives 12.
output:
$ javac operators.java
$ java operators
12
7. What is the output of this program?
-
class operators
-
{
-
public static void main(String args[])
-
{
-
int x = 8;
-
System.out.println(++x * 3 + " " + x);
-
}
-
}
a) 24 8
b) 24 9
c) 27 8
d) 27 9
Answer
Answer: d [Reason:] Operator ++ has higher precedence than multiplication operator, *, x is incremented to 9 than multiplied with 3 giving 27.
output:
$ javac operators.java
$ java operators
27 9
8. What is the output of this program?
-
class Output
-
{
-
public static void main(String args[])
-
{
-
int x=y=z=20;
-
-
}
-
}
a) compile and runs fine
b) 20
c) run time error
d) compile time error
Answer
Answer:d [Reason:] None.
9. Which of these lines of code will give better performance?
1. a | 4 + c >> b & 7; 2. (a | ((( 4 * c ) >> b ) & 7 ))
a) 1 will give better performance as it has no parentheses.
b) 2 will give better performance as it has parentheses.
c) Both 1 & 2 will give equal performance.
d) Dependent on the computer system.
Answer
Answer: c [Reason:] Parentheses do not degrade the performance of the program. Adding parentheses to reduce ambiguity does not negatively affect your system.
10. What is the output of this program?
-
class Output
-
{
-
public static void main(String args[])
-
{
-
int a,b,c,d;
-
a=b=c=d=20
-
a+=b-=c*=d/=20
-
System.out.println(a+" "+b+" "+c+" "+d);
-
-
}
-
}
a) compile time error
b) runtime error
c) a=20 b=0 c=20 d=1
d) none of the mentioned
Answer
Answer: c [Reason:] Expression will evaluate from right to left.
output:
$ javac Output.java
$ java Output
20 0 20 1
Java MCQ Set 4
1. Which of these methods of Boolean wrapper returns boolean equivalent of an object.
a) getBool()
b) booleanValue()
c) getbooleanValue()
d) getboolValue()
Answer
Answer: b [Reason:] None.
2. Which of the following constant are defined in Boolean wrapper?
a) TRUE
b) FALSE
c) TYPE
d) All of the mentioned
Answer
Answer: d [Reason:] Boolean wrapper defines 3 constants – TRUE, FALSE & TYPE.
3. Which of these methods return string equivalent of Boolean object?
a) getString()
b) toString()
c) converString()
d) getStringObject()
Answer
Answer: b [Reason:] None.
4. Which of these methods is used to know whether a string contains “true”?
a) valueOf()
b) valueOfString()
c) getString()
d) none of the mentioned
Answer
Answer: a [Reason:] valueOf() returns true if the specified string contains “true” in lower or uppercase and false otherwise.
5. Which of these class have only one field?
a) Character
b) Boolean
c) Byte
d) void
Answer
Answer: d [Reason:] Void class has only one field – TYPE, ehich holds a reference to the Class object for type void. We do not create instance of this class.
6. What is the output of this program?
-
class Output
-
{
-
public static void main(String args[])
-
{
-
String str = "true";
-
boolean x = Boolean.valueOf(str);
-
System.out.print(x);
-
}
-
}
a) True
b) False
c) Compilation Error
d) Runtime Error
Answer
Answer: a [Reason:] valueOf() returns true if the specified string contains “true” in lower or uppercase and false otherwise.
Output:
$ javac Output.java
$ java Output
true
7. What is the output of this program?
-
class Output
-
{
-
public static void main(String args[])
-
{
-
String str = "true false true";
-
boolean x = Boolean.valueOf(str);
-
System.out.print(x);
-
}
-
}
a) True
b) False
c) Compilation Error
d) Runtime Error
Answer
Answer: b [Reason:] valueOf() returns true if the specified string contains “true” in lower or uppercase and false otherwise.
Output:
$ javac Output.java
$ java Output
false
8. What is the output of this program?
-
class Output
-
{
-
public static void main(String args[])
-
{
-
String str = "TRUE";
-
boolean x = Boolean.valueOf(str);
-
System.out.print(x);
-
}
-
}
a) True
b) False
c) Compilation Error
d) Runtime Error
Answer
Answer: a [Reason:] valueOf() returns a Boolean instance representing the specified boolean value. If the specified boolean value is true, this method returns Boolean.TRUE; if it is false, this method returns Boolean.FALSE. If a new Boolean instance is not required, this method should generally be used in preference to the constructor Boolean(boolean), as this method is likely to yield significantly better space and time.
Output:
$ javac Output.java
$ java Output
true
9. What is the output of this program?
-
class Output
-
{
-
public static void main(String args[])
-
{
-
String str = "true false";
-
boolean x = Boolean.parseBoolean(str);
-
System.out.print(x);
-
}
-
}
a) True
b) False
c) System Dependent
d) Compilation Error
Answer
Answer: b [Reason:] parseBoolean() Parses the string argument as a boolean. The boolean returned represents the value true if the string argument is not null and is equal, ignoring case, to the string “true”.
Example: Boolean.parseBoolean(“True”) returns true.
Example: Boolean.parseBoolean(“yes”) returns false.
Output:
$ javac Output.java
$ java Output
false
10. What is the output of this program?
-
class Output
-
{
-
public static void main(String args[])
-
{
-
String x = Boolean.toString(false);
-
}
-
}
a) True
b) False
c) System Dependent
d) Compilation Error
Answer
Answer: b [Reason:] toString() Returns a String object representing the specified boolean. If the specified boolean is true, then the string “true” will be returned, otherwise the string “false” will be returned
Output:
$ javac Output.java
$ java Output
false
Java MCQ Set 5
1. Which of these exceptions handles the situations when illegal argument is used to invoke a method?
a) IllegalException
b) Argument Exception
c) IllegalArgumentException
d) IllegalMethodArgumentExcepetion
Answer
Answer: c [Reason:] None.
2. Which of these exceptions will be thrown if we declare an array with negative size?
a) IllegalArrayException
b) IllegalArraySizeExeption
c) NegativeArrayException
d) NegativeArraySizeExeption
Answer
Answer: d [Reason:] Array size must always be positive, if we declare an array with negative size then built in exception “NegativeArraySizeException” is thrown by the java’s run time system.
3. Which of these packages contain all the Java’s built in exceptions?
a) java.io
b) java.util
c) java.lang
d) java.net
Answer
Answer: c [Reason:] None.
4. Which of these exceptions will be thrown if we use null reference for an arithmetic operation?
a) ArithmeticException
b) NullPointerException
c) IllegalAccessException
d) IllegalOperationException
Answer
Answer: b [Reason:] If we use null reference anywhere in the code where the value stored in that reference is used then NullPointerException occurs.
5. Which of these class is used to create user defined exception?
a) java.lang
b) Exception
c) RunTime
d) System
Answer
Answer: b [Reason:] Exception class contains all the methods necessary for defining an exception. The class contains the Throwable class.
6. What is the output of this program?
-
class exception_handling
-
{
-
public static void main(String args[])
-
{
-
try
-
{
-
int a[] = {1, 2,3 , 4, 5};
-
for (int i = 0; i < 7; ++i)
-
System.out.print(a[i]);
-
}
-
catch(ArrayIndexOutOfBoundsException e)
-
{
-
System.out.print("0");
-
}
-
}
-
}
a) 12345
b) 123450
c) 1234500
d) Compilation Error
Answer
Answer: b [Reason:] When array index goes out of bound then ArrayIndexOutOfBoundsException exception is thrown by the system.
Output:
$ javac exception_handling.java
$ java exception_handling
123450
7. What is the output of this program?
-
class exception_handling
-
{
-
public static void main(String args[])
-
{
-
try
-
{
-
int a[] = {1, 2,3 , 4, 5};
-
for (int i = 0; i < 5; ++i)
-
System.out.print(a[i]);
-
int x = 1/0;
-
}
-
catch(ArrayIndexOutOfBoundsException e)
-
{
-
System.out.print("A");
-
}
-
catch(ArithmeticException e)
-
{
-
System.out.print("B");
-
}
-
}
-
}
a) 12345
b) 12345A
c) 12345B
d) Comiplation Error
Answer
Answer: d [Reason:] There can be more than one catch of a single try block. Here Arithmetic exception occurs instead of Array index out of bound exception hence B is printed after 12345
Output:
$ javac exception_handling.java
$ java exception_handling
12345B
8. What is the output of this program?
-
class exception_handling
-
{
-
static void throwexception() throws ArithmeticException
-
{
-
System.out.print("0");
-
throw new ArithmeticException ("Exception");
-
}
-
public static void main(String args[])
-
{
-
try
-
{
-
throwexception();
-
}
-
catch (ArithmeticException e)
-
{
-
System.out.println("A");
-
}
-
}
-
}
a) A
b) 0
c) 0A
d) Exception
Answer
Answer: c [Reason:] None.
Output:
$ javac exception_handling.java
$ java exception_handling
0A
9. What is the output of this program?
-
class exception_handling
-
{
-
public static void main(String args[])
-
{
-
try
-
{
-
int a = 1;
-
int b = 10 / a;
-
try
-
{
-
if (a == 1)
-
a = a / a - a;
-
if (a == 2)
-
{
-
int c[] = {1};
-
c[8] = 9;
-
}
-
finally
-
{
-
System.out.print("A");
-
}
-
-
}
-
catch (NullPointerException e)
-
{
-
System.out.println("B");
-
}
-
}
-
}
a) A
b) B
c) AB
d) BA
Answer
Answer: c [Reason:] The inner try block does not have a catch which can tackle ArrayIndexOutOfBoundException hence finally is executed which prints ‘A’ the outer try block does have catch for NullPointerException exception but no such exception occurs in it hence its catch is never executed and only ‘A’ is printed.
Output:
$ javac exception_handling.java
$ java exception_handling
A
10. What is the output of this program?
-
class exception_handling
-
{
-
public static void main(String args[])
-
{
-
try
-
{
-
int a = args.length;
-
int b = 10 / a;
-
System.out.print(a);
-
try
-
{
-
if (a == 1)
-
a = a / a - a;
-
if (a == 2)
-
{
-
int c = {1};
-
c[8] = 9;
-
}
-
}
-
catch (ArrayIndexOutOfBoundException e)
-
{
-
System.out.println("TypeA");
-
}
-
catch (ArithmeticException e)
-
{
-
System.out.println("TypeB");
-
}
-
}
-
}
a) TypeA
b) TypeB
c) 0TypeA
d) 0TypeB
Note: Execution command line: $ java exception_handling one two
Answer
Answer: c [Reason:] Execution command line is “$ java exception_ handling one two” hence there are two input making args.length = 2, hence “c[8] = 9” in second try block is executing which throws ArrayIndexOutOfBoundException which is caught by catch of nested try block. Hence 0TypeB is printed
Output:
$ javac exception_handling.java
$ java exception_handling
0TypeB