0% found this document useful (0 votes)
3K views

Java MCQ

The document contains multiple choice questions about object-oriented programming concepts in Java. It asks about objects, classes, inheritance, encapsulation, packages, exceptions, threads, and data types. The last question asks about what kind of thread the Garbage Collector thread is, with the answer being that it is a Daemon thread.

Uploaded by

Saqib Khawaja
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3K views

Java MCQ

The document contains multiple choice questions about object-oriented programming concepts in Java. It asks about objects, classes, inheritance, encapsulation, packages, exceptions, threads, and data types. The last question asks about what kind of thread the Garbage Collector thread is, with the answer being that it is a Daemon thread.

Uploaded by

Saqib Khawaja
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Which of the following statements is false about objects?

An instance of a class is an object

Objects can access both static and


instance data

Object is the super class of all other


classes

Objects do not permit encapsulation

Which methods can access to private attributes of a class?

Only Static methods of the same


class

Only instances of the same class

Only methods those defined in the


same class

Only classes available in the same


package.

What is an aggregate object?

An object with only primitive attributes

An instance of a class which has only


static methods

An instance which has other objects

None of the above

Assume that File is an abstract class and has toFile() method. ImageFile and
BinaryFile are concrete classes of the abstract class File.
Also, assume that the method toFile() is implemented in both Binary File and
Image File. A File references an ImageFile object in memory and the toFile
method is called, which implementation method will be called?

Binary File

Image File

Both File and Binary


Files

None of the above

A class can have many methods with the same name as long as the number of
parameters or type of parameters is different. This OOP concept is known as

Method
Invocating

Method
Overriding

Method Labeling

Method
Overloading

Which of the following is considered as a blue print that defines the variables and
methods common to all of its objects of a specific kind?

Object

Class

Method
Real data
types

What are the two parts of a value of type double?

Significant Digits,
Exponent

Length, Denominator

Mode, Numerator

After the following code fragment, what is the value in fname?

String str;
int fname;
str = "Foolish boy.";
fname = str.indexOf("fool");

-1

What is the value of ‘number’ after the following code fragment execution?

int number = 0;
int number2 = 12
while (number < number2)
{
number = number + 1;
}

12
21

13
Given the following code snippet;

int salaries[];
int index = 0;
salaries = new int salaries[4];
while (index < 4)
{
salaries[index] = 10000;
index++;
}
What is the value of salaries [3]?

4000
0

5000
0

1500
0

1000
0

Which of the following is not a return type?

boolea
n

void

public

Button

If result = 2 + 3 * 5, what is the value and type of ‘result’ variable?


17,
byte

25,
byte

17, int

25, int

What is the data type for the number 9.6352?

float

doubl
e

Float

Doubl
e

Assume that the value 3929.92 is of type ‘float’. How to assign this value after
declaring the variable ‘interest’ of type float?

interest = 3929.92

interest =
(Float)3929.92

interest = 3929.92
(float)

interest = 3929.92f

Which of the following statements is true?


The default char data type is a space( ‘ ‘ ) character.

The default integer data type is ‘int’ and real data


type is ‘float’

The default integer data type is ‘long’ and real data


type is ‘float’

The default integer data type is ‘int’ and real data


type is ‘double’

How many numeric data types are supported in Java?

package payroll;package
admindept;

import payroll.*;

package
payroll.admindept.Sample;

import payroll.admindept.*;

package payroll.admindept;

The class java.lang.Exception is


protected

extends Throwable

implements
Throwable

serializable

Which of the following statements is true?

An exception can be thrown by throw keyword


explicitly.

An exception can be thrown by throws


keyword explicitly.

All the wrapper classes (Integer, Boolean, Float, Short, Long, Double and
Character) in java

are private

are
serializable

are
immutatable

are final

he code snippet

if( "Welcome".trim() == "Welcome".trim() )


System.out.println("Equal");
else
System.out.println("Not Equal");

will
compile and display
“Equal”

compile and display “Not


Equal”

cause a compiler error

compile and display NULL

Consider the following code snippet. What will be assigned to the variable
fourthChar, if the code is executed?

String str = new String(“Java”);


char fourthChar = str.charAt(4);

‘a’

‘v’

throws
StringIndexOutofBoundsException

null characater

Which of the following statements is preferred to create a string "Welcome to


Java Programming"?

String str = “Welcome to Java Programming”

String str = new String( “Welcome to Java


Programming” )

String str; str = “Welcome to Java Programming”

String str; str = new String (“Welcome to Java


Programming” )

Which of the following statements is true?

A super class is a sub set of a sub class

class ClassTwo extends ClassOne means ClassOne is


a subclass

class ClassTwo extends ClassOne means ClassTow is


a super class

the class Class is the super class of all other classes


in Java.

What kind of thread is the Garbage collector thread is?

Non daemon
thread

Daemon thread

Thread with dead


state

None of the above

When a thread terminates its processing, into what state that thread enters?

Running
state

Waiting
state

Dead state

Beginning s
0. Which statement is true?

HashTable is a sub class of


Dictionary

ArrayList is a sub class of


Vector

LinkedList is a subclass of
ArrayList

Vector is a subclass of Stack

Which of the methods should be implemented if any class implements the


Runnable interface?

start()

run()

wait()

notify() and
notifyAll()

A thread which has invoked wait() method of an object, still owns the lock of the
object. Is this statement true or false?

True

Fals
e

Which of the following is not a method of the Thread class.

public void run()


public void start()

public void exit()

public final int


getPriority()

To execute the threads one after another

the keyword synchronize is


used

the keyword synchronizable


is used

the keyword synchronized is


used

None of the above

DataInputStream is an example of

Output
stream

I/O stream

Filtered
stream

File stream

What is the meaning of the return data type void?


You answered: An empty memory space is returned so that the developers can utilize it.
Incorrect

2. A lower precision can be assigned to a higher precision value in Java. For example a byte type data
can be assigned to int type.
Correct

3. Which of the following statements about the Java language is true?


You answered: Java supports only OOP approach.
Incorrect

4. Which of the following statements is false about objects?


Correct

5. Which methods can access to private attributes of a class?


You answered: Only instances of the same class
Incorrect

6. What is an aggregate object?


Correct

7. Assume that File is an abstract class and has toFile() method. ImageFile and BinaryFile are concrete
classes of the abstract class File.

Also, assume that the method toFile() is implemented in both Binary File and Image File. A File
references an ImageFile object in memory and the toFile method is called, which implementation method
will be called?
Correct

8. A class can have many methods with the same name as long as the number of parameters or type of
parameters is different. This OOP concept is known as
You answered: Method Labeling
Incorrect

9. Which of the following is considered as a blue print that defines the variables and methods common to
all of its objects of a specific kind?
Correct

10. What are the two parts of a value of type double?


You answered: Length, Denominator
Incorrect

11. After the following code fragment, what is the value in fname?

String str;
int fname;
str = "Foolish boy.";
fname = str.indexOf("fool");
Correct

12. What is the value of ‘number’ after the following code fragment execution?

int number = 0;
int number2 = 12
while (number < number2)
{
number = number + 1;
}
You answered: 21
Incorrect
13. Given the following code snippet;

int salaries[];
int index = 0;
salaries = new int salaries[4];
while (index < 4)
{
salaries[index] = 10000;
index++;
}
What is the value of salaries [3]?
You answered: 50000
Incorrect

14. Which of the following is not a return type?


You answered: Button
Incorrect

15. If result = 2 + 3 * 5, what is the value and type of ‘result’ variable?


Correct

16. What is the data type for the number 9.6352?


Correct

17. Assume that the value 3929.92 is of type ‘float’. How to assign this value after declaring the variable
‘interest’ of type float?
You answered: interest = (Float)3929.92
Incorrect

18. Which of the following statements is true?


Correct

19. How many numeric data types are supported in Java?


You answered: 2
Incorrect

20. Which of the following statements declare class Sample to belong to the payroll.admindept package?
Correct

21. The class java.lang.Exception is


You answered: implements Throwable
Incorrect

22. Which of the following statements is true?


Correct

23. All the wrapper classes (Integer, Boolean, Float, Short, Long, Double and Character) in java
You answered: are immutatable
Incorrect

24. The code snippet

if( "Welcome".trim() == "Welcome".trim() )


System.out.println("Equal");
else
System.out.println("Not Equal");
will
Correct

25. Consider the following code snippet. What will be assigned to the variable fourthChar, if the code is
executed?

String str = new String(“Java”);


char fourthChar = str.charAt(4);
Correct

26. Which of the following statements is preferred to create a string "Welcome to Java Programming"?
You answered: String str; str = new String (“Welcome to Java Programming” )
Incorrect

27. Which of the following statements is true?


You answered: the class Class is the super class of all other classes in Java.
Incorrect

28. What kind of thread is the Garbage collector thread is?


You answered: None of the above
Incorrect

29. When a thread terminates its processing, into what state that thread enters?
You answered: Beginning state
Incorrect

30. Which statement is true?


You answered: LinkedList is a subclass of ArrayList
Incorrect

31. Which of these statements is true?


You answered: AbstractSet extends Set
Incorrect

32. Which of the following is synchronized?


Correct

33. Select all the true statements from the following.


Correct

34. Which of the methods should be implemented if any class implements the Runnable interface?
You answered: wait()
Incorrect

35. A thread which has invoked wait() method of an object, still owns the lock of the object. Is this
statement true or false?
Correct

36. Which of the following is not a method of the Thread class.


You answered: public final int getPriority()
Incorrect

37. To execute the threads one after another


You answered: None of the above
Incorrect
38. The object of DataInputStream is used to
You answered: All of the above
Incorrect

39. DataInputStream is an example of


Correct

You might also like