Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
34 views

Java MCQ

Uploaded by

kush6191
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Java MCQ

Uploaded by

kush6191
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

SYNERGY INSTITUTE OF TECHNOLOGY, BHUBANESWAR

DEPT-COMPUTER SCIENCE
SUB-JAVA
PREPARED BY-Assistant prof. TAPASI DAS

Java Multiple Choice Questions with short questions & answers

1.The Java interpreter is used for the execution of the source code.
True
False
Ans: a.

2) On successful compilation a file with the class


extension is created.
a) True b) False Ans: a.
3) The Java source code can be created in a Notepad editor.
a)True b) False Ans: a.

4) The Java Program is enclosed in a class definition.


a)True b)False Ans: a.
5)What declarations are required for every
Java application?
Ans: A class and the main( ) method
declarations.
6) What are the two parts in executing a Java
program and their purposes? Ans: Two parts in
executing a Java program are:

Java Compiler and Java Interpreter.

The Java Compiler is used for compilation and the Java Interpreter
is used for execution of the application.
7) What are the three OOPs principles and define them?

Ans : Encapsulation, Inheritance and


Polymorphism are the three OOPs Principles.

Encapsulation:

Is the Mechanism that binds together code and the data it


manipulates, and keeps both safe from outside interference and
misuse.

Inheritance:

Is the process by which one object acquires the


properties of another object. Polymorphism:

Is a feature that allows one interface to be used for a general class of actions.

8) What is a
compilation unit?
Ans : Java source
code file.
9) What output is displayed as the result of executing
the following statement? System.out.println("// Looks
like a comment.");
// Looks like a comment

The statement results in a


compilation error Looks like a
comment

No output is
displayed
Ans : a.

10) In order for a source code file, containing the public class
Test, to successfully compile, which of the following must be true?
It must have a package
statement It must be
named Test.java
It must import java.lang

It must declare a public class


named Test
Ans : b

Data types,variables and Arrays


11)Java supports
multidimensional arrays.

a)True b)FlseAns: a.
12)An array of arrays can be created.
a)True b)False
Ans: a.
13) What is a string?

Ans: A combination of characters is called as string.

14) Strings are instances of the


class String.

a)True b)False

15)When a string literal is used in the program, Java automatically creates


instances of the string class.
a)Trueb)False Ans: a.
16)What is the value of a[3] as the result of the following array declaration?
1
2
3
4
Ans : d
17)Which of the following are primitive types?
byte

String
integer
Float
Ans : a.

18) What are primitive


data types?
Ans : byte, short, int,
longfloat, double
boolean char

19) What are default values of different


primitive types? Ans : int - 0
short - 0
byte - 0
long - 0 l
float - 0.0
f double -
0.0 d
boolean -
false char
- null

20) Converting of primitive types to objects


can be done explicitly.
a)True b)FALSE
ans b
Operators
21)The ++ operator is used for incrementing and the
-- operator is used for decrementing.
a)True b)False Ans: a.
1) Comparison/Logical operators are used for testing and magnitude.
a)True
b)False
Ans: a.

22)Character literals are stored as


unicode characters.
a)True b)False Ans: a.
23)What are the Logical operators?
Ans: OR(|), AND(&), XOR(^) AND NOT(~).
24) What is the % operator?
Ans : % operator is the modulo operator or reminder operator. It returns
the reminder of dividing the first operand by second operand.

25)What is the value of 111 % 13?


3
5
7
9

Ans : c.
26)Can a double value be
cast to a byte?
a)yes
b)no
Ans : Yes
27)Can a byte object be cast to a double value ?
a)yes
b)no
Ans : No. An object cannot be cast to a primitive value.
28) What are order of precedence and associativity?
Ans : Order of precedence the order in which operators are evaluated in
expressions.
Associativity determines whether an expression is evaluated left-right or right-
left.

29) What are the values of x and y ?


x = 5; y = ++x;
Ans : x = 6; y = 6
30)What are the values
of x and z?
x = 5; z = x++;
Ans : x = 6; z = 5
Control Statements

31)The switch statement does not


require a break.
a)True b)False Ans: b.
32)The conditional operator is otherwise known
as the ternary operator.
a)True b)False Ans: a.

33)The while loop repeats a set of code while


the condition is false.

a)True b)False Ans: b.


34)The do-while loop repeats a set of code atleast once
before the condition is tested.
a)True
b)False Ans:yes
Introduction to Classes and Methods

35)The new operator creates a single instance


named class and returns a reference to that object.
a)True b)False Ans: a.
1) A class is a template for multiple objects
with similar features.
a)True b)False Ans: a.

36)Casting between primitive types allows conversion of one


primitive type to another.
a)Trueb)False Ans: a.

37)Casting occurs commonly between numeric types.


a)True
b)False

Ans: a.
38)Boolean values can be cast into any
other primitive type.
a)True b)False Ans: b.

39)Casting does not affect the original


object or value.
a)True b)False Ans:a

40)Which of the following types of class members can be part of


the internal part of a class?
a.Public instance variables
b.Private instance
variables
c.Public methods
d.Private methods

Ans: b,d.
41)What is an example of polymorphism?
Inner class

Anonymous classes
Method overloading
Method overriding
Ans : c

Packages and interface


42)User-defined package can also be imported just like
the standard packages. True/False
Ans : True

43)When a program does not want to handle exception,


the ______class is used.
Ans : Throws

44)Only subclasses of ______class may be caught or thrown.


Ans : Throwable
45) Any user-defined exception class is a
subclass of the _____ class.
Ans : Exception

46)A _______ is used to separate the hierarchy of the


class while declaring an Import statement.
Ans : Package
47)All standard classes of Java are included within a
package called _____.
Ans : java.lang
48)All the classes in a package can be simultaneously
imported using ____.
Ans : *

Exception Handling
49)The finally block is executed when an exception is thrown,
even if no catch matches it. True/False

Dept of Computer Science SIT


Java Multiple Choice Questions

Ans : True

50)The subclass exception should precede the base class exception when
used within the catch clause. True/False
Ans : True
51)The statements following the throw keyword in a
program are not executed. True/False
Ans : True

52)The toString ( ) method in the user-defined


exception class is overridden. True/False
Ans : True

MULTI THREADING
53)What are the two types of
multitasking?
Ans : 1.process-based
2.Thread-based

54)What are the two ways to


create the thread?
Ans : 1.by implementing
Runnable
2.by extending Thread

55)What is the signature of the constructor


of a thread class?
Ans : Thread(Runnable threadob,String
threadName)
56)What are all the methods available in the
Runnable Interface?
Ans : run()
57)What is the data type for the method
isAlive() and this method is available in which
class?
Ans : boolean, Thread

58)What are all the methods available in the Thread class?


Ans : 1.isAlive()
2.join()
3.resume()
4.suspend()

5.stop()
6.start()
7.sleep()
8.destroy()

59)What are all the methods used for Inter Thread communication and
what is the class in which these methods are defined?
Ans :1. wait(),notify() & notifyall()
2. Object class

60)What is the mechanisam defind by java for the Resources to be used


by only one Thread at a time?
Ans : Synchronisation
61)What is the unit for 1000 in the
below statement? ob.sleep(1000)
Ans : long milliseconds

62)What is the data type for the parameter of


the sleep() method? Ans : long
63)What are all the values for the following level?
max-priority
min-priority
normal-priority
Ans : 10,1,5
64) What is the method available for
setting the priority?
Ans : setPriority()
65)What is the default thread at the time of
starting the program?
Ans : main thread
66)The word synchronized can be used
with only a method. True/ False
Ans : False

67) What are all the four states


associated in the thread?
Ans : 1. new 2. runnable 3. blocked
4. dead
68)The suspend()method is used to
teriminate a thread? True /False
Ans : False

69)The run() method should necessary exists in clases


created as subclass of thread? True /False

Ans : True
70)Garbage collector thread belongs to
which priority? Ans : low-priority

Inheritance

71) What is the difference between superclass & subclass?

Ans : A super class is a class that is inherited whereas subclass is a class that
does the inheriting.

72)Which keyword is used to


inherit a class? Ans : extends
73) Object class is a superclass of
all other classes? True/False
Ans : True
74)Java supports multiple inheritance?
True/False
Ans : False
75) What is inheritance?

Ans : Deriving an object from an existing class. In the other words,


Inheritance is the process of inheriting all the features from a class

76)What are the advantages of inheritance?


Ans : Reusability of code and accessibility of variables and methods of the
superclass by subclasses.

77)Which methods are used to destroy the objects created by


the constructor methods? Ans : finalize()
78)What are abstract classes?
Ans : Abstract classes are those for which instances can’t be created.
79)What must a class do to implement an interface?

Ans: It must provide all of the methods in the interface and identify

STRING HANDLING

80)What is the output of the following program?


public class Question {
public static void main(String args[]) {

String s1 = "abc";
String s2 = "def";
String s3 = s1.concat(s2.toUpperCase( ) );
System.out.println(s1+s2+s3);
}
}

a)abcdefabcdef
b)abcabcDEFDEF
c)abcdefabcDEF
d)None of the above
ANS : c.
81)Which of the following methods are methods of the String class?

a)delete( )
b)append( )
c)reverse( )
d)replace( )
Ans : d.

82)Which of the following methods cause the String object referenced by s to


be changed?
a)s.concat( )

b)s.toUpperCase( )
c)s.replace( )
d)s.valueOf( )
Ans : a and b.

83)String is a wrapper class?


a)True
b)False
Ans : b.
APPLETS
84)What is an Applet? Should applets have constructors?

Ans : Applet is a dynamic and interactive program that runs inside a Web page
displayed by a Java capable browser. We don’t have the concept of
Constructors in Applets.

85) How can I arrange for different applets on a web page to


communicate with each other? Ans : Name your applets inside
the Applet tag and invoke AppletContext’s getApplet() method in
your applet code to obtain references to the other applets on the
page.
86)How do I select a URL from my Applet and send the browser to that page?

Ans : Ask the applet for its applet context and invoke showDocument() on that
context object.
Eg. URL targetURL;
String URLString
AppletContext context = getAppletContext();
try{

targetUR L = new URL(URLString);

} catch (Malformed
URLException e){ // Code
for recover from the
exception
}
context. showDocument (targetURL);
87)Can applets on different pages communicate with each other?

Ans : No. Not Directly. The applets will exchange the


information at one meeting place either on the local file
system or at remote system.
88)How can I arrange for different applets on a web page to
communicate with each other? Ans : Name your applets inside
the Applet tag and invoke AppletContext’s getApplet() method in
your applet code to obtain references to the other applets on the
page.
89)How do I select a URL from my Applet and send the browser to that page?

Ans : Ask the applet for its applet context and invoke showDocument() on that
context object.
Eg. URL targetURL;
String URLString
AppletContext context = getAppletContext();
try{

targetUR L = new URL(URLString);

} catch (Malformed
URLException e){ // Code
for recover from the
exception
}
context. showDocument (targetURL);
90)Can applets on different pages communicate with each other?

Ans : No. Not Directly. The applets will exchange the


information at one meeting place either on the local file
system or at remote system.

91)How do Applets differ


from Applications?
Ans : Appln: Stand Alone
Applet: Needs no explicit installation on local m/c.
Appln: Execution starts with main() method.

Applet: Execution starts with init() method.


Appln: May or may not be a GUI
Applet: Must run within a GUI (Using AWT)

92)What are the Applet’s Life Cycle methods? Explain them?


Ans : init( ) method - Can be called when an applet is first loaded.

Java Multiple Choice Questions

start( ) method - Can be called each time an applet is started.

paint( ) method - Can be called when the applet is


minimized or refreshed. stop( ) method - Can be called
when the browser moves off the applet’s page. destroy(
) method - Can be called when the browser is finished
with the applet. What are the Applet’s information
methods?

Ans : getAppletInfo( ) method : Returns a string describing


the applet, its author ,copy right information, etc.

getParameterInfo( ) method : Returns an array of string describing the applet’s


parameters.
93)All Applets are subclasses of Applet.
True.

False.
Ans : a.
94)All Applets must import java.applet and java.awt.
True.
False.
Ans : a.

EVENT HANDLING

95) Which of the following components generate action events?

a.Buttons
b.Labels
c.Check boxes
Ans : a.
96) Which of the following are true?
a.The MouseListener interface defines methods for handling mouse clicks.
b.The MouseMotionListener interface defines methods for handling mouse
clicks.
c.The MouseClickListener interface defines methods for handling mouse clicks.
d.The ActionListener interface defines methods for handling the clicking of a
button.

Ans : a and d.

97)Suppose that you want to have an object eh handle the TextEvent of


a TextArea object t. How should you add eh as the event handler for t?

a.t.addTextListener(eh);
b.eh.addTextListener(t);
c.addTextListener(eh.t);

d.addTextListener(t,eh);
Ans : a.
98)What is the preferred way to handle an object’s events in Java 2?
a.Override the object’s handleEvent( ) method.
b.Add one or more event listeners to handle the events.
c.Have the object override its processEvent( ) methods.

d.Have the object override its dispatchEvent( ) methods.


Ans : b.
99) What event results from the clicking of a button?

Ans : The ActionEvent event is generated as the result of the

100) Which of the following is the highest class in the event-delegation model?
a.java.util.EventListener
b.java.util.EventObject

c.d.java.awt.AWTEvent
d.java.awt.event.AWTEvent
Ans : b

You might also like