KEMBAR78
Java Core-100 MCQ Questions | PDF | Class (Computer Programming) | Method (Computer Programming)
0% found this document useful (0 votes)
10 views32 pages

Java Core-100 MCQ Questions

Java MCQ questions

Uploaded by

Ms R Dhivya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views32 pages

Java Core-100 MCQ Questions

Java MCQ questions

Uploaded by

Ms R Dhivya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

MMC202 – Object Oriented Programming using Java

1. Which of this class is superclass of String and StringBuffer class?


a) java.util
b) java.lang
c) ArrayList
d) None of the mentioned
Ans: java.lang
2. Which of these operators can be used to concatenate two or more String objects?
a) +
b) +=
c) &
d) ||
Ans: +

3. Which of this method of class String is used to obtain a length of String object?
a) get()
b) Sizeof()
c) lengthof()
d) length()
Ans: length()

4. Which of these method of class String is used to extract a single character from a
String object?
a) CHARAT()
b) chatat()
c) charAt()
d) ChatAt()
Ans: charAt()

5. Which of these constructors is used to create an empty String object?


a) String()
b) String(void)
c) String(0)
d) None of the mentioned
Ans: String()
6. Which of these is an incorrect statement?
a) String objects are immutable, they cannot be changed
b) String object can point to some other reference of String variable
c) StringBuffer class is used to store string in a buffer for later use
d) None of the mentioned
Ans: StringBuffer class is used to store string in a buffer for later use

7. What will be the output of the following Java program?

1. class String_demo
2. {
3. public static void main(String args[])
4. {
5. char chars[] = {'a', 'b', 'c'};
6. String s = new String(chars);
7. System.out.println(s);
8. }
9. }

a) a
b) b
c) c
d) abc
Ans: abc

8. What will be the output of the following Java program?

1. class String_demo
2. {
3. public static void main(String args[])
4. {
5. int ascii[] = { 65, 66, 67, 68};
6. String s = new String(ascii, 1, 3);
7. System.out.println(s);
8. }
9. }

a) ABC
b) BCD
c) CDA
d) ABCD
Ans: BCD

9. What will be the output of the following Java program?

1. class String_demo
2. {
3. public static void main(String args[])
4. {
5. char chars[] = {'a', 'b', 'c'};
6. String s = new String(chars);
7. String s1 = "abcd";
8. int len1 = s1.length();
9. int len2 = s.length();
10. System.out.println(len1 + " " + len2);
11. }
12. }

a) 3 0
b) 0 3
c) 3 4
d) 4 3

Ans: 4 3

10. Which of these selection statements test only for equality?


a) if
b) switch
c) if & switch
d) none of the mentioned
Ans: switch

11. Which of these are selection statements in Java?


a) if()
b) for()
c) continue
d) break
Ans: if()

12. Which of the following loops will execute the body of loop even when condition
controlling the loop is initially false?
a) do-while
b) while
c) for
d) none of the mentioned
Ans: do-while

13. Which of these jump statements can skip processing the remainder of the code in its
body for a particular iteration?
a) break
b) return
c) exit
d) continue
Ans: continue

14. Which of this statement is incorrect?


a) switch statement is more efficient than a set of nested ifs
b) two case constants in the same switch can have identical values
c) switch statement can only test for equality, whereas if statement can evaluate any
type of boolean expression
d) it is possible to create a nested switch statements
Ans: two case constants in the same switch can have identical values

15. What will be the output of the following Java program?

1. class selection_statements
2. {
3. public static void main(String args[])
4. {
5. int var1 = 5;
6. int var2 = 6;
7. if ((var2 = 1) == var1)
8. System.out.print(var2);
9. else
10. System.out.print(++var2);
11. }
12. }
a) 1
b) 2
c) 3
d) 4
Ans: 2

16. What will be the output of the following Java program?

1. class comma_operator
2. {
3. public static void main(String args[])
4. {
5. int sum = 0;
6. for (int i = 0, j = 0; i < 5 & j < 5; ++i, j = i + 1)
7. sum += i;
8. System.out.println(sum);
9. }
10. }
a) 5
b) 6
c) 14
d) compilation error
Ans: 6

17. What will be the output of the following Java program?

1. class jump_statments
2. {
3. public static void main(String args[])
4. {
5. int x = 2;
6. int y = 0;
7. for ( ; y < 10; ++y)
8. {
9. if (y % x == 0)
10. continue;
11. else if (y == 8)
12. break;
13. else
14. System.out.print(y + " ");
15. }
16. }
17. }
a) 1 3 5 7
b) 2 4 6 8
c) 1 3 5 7 9
d) 1 2 3 4 5 6 7 8 9
Ans: 1 3 5 7 9

18. What will be the output of the following Java program?

1. class Output
2. {
3. public static void main(String args[])
4. {
5. final int a=10,b=20;
6. while(a<b)
7. {
8.
9. System.out.println("Hello");
10. }
11. System.out.println("World");
12.
13. }
14. }
a) Hello
b) run time error
c) Hello world
d) compile time error
Ans: compile time error

19. What will be the output of the following Java program?


1. class Output
2. {
3. public static void main(String args[])
4. {
5. int a = 5;
6. int b = 10;
7. first:
8. {
9. second:
10. {
11. third:
12. {
13. if (a == b >> 1)
14. break second;
15. }
16. System.out.println(a);
17. }
18. System.out.println(b);
19. }
20. }
21. }
a)5 10
b) 10 5
c) 5
d) 10
Ans: 10

20 . Which of the following has the highest memory requirement?


a) Heap
b) Stack
c) JVM
d) Class

Ans: JVM

21. Which of this keyword must be used to inherit a class?


a) super
b) this
c) extent
d) extends
Answer : extends

22. A class member declared protected becomes a member of subclass of which type?
a) public member
b) private member
c) protected member
d) static member
Answer: private member

23. Which of these is correct way of inheriting class A by class B?


a) class B + class A {}
b) class B inherits class A {}
c) class B extends A {}
d) class B extends class A {}
Answer: class B extends A {}

24. Which two classes use the Shape class correctly?


A. public class Circle implements Shape
{
private int radius;
}
B. public abstract class Circle extends Shape
{
private int radius;
}
C. public class Circle extends Shape
{
private int radius;
public void draw();
}
D. public abstract class Circle implements Shape
{
private int radius;
public void draw();
}
E. public class Circle extends Shape
{
private int radius;
public void draw()
{
/* code here */
}
}
F. public abstract class Circle implements Shape
{
private int radius;
public void draw()
{
/* code here */
}
}

a) B,E
b) A,C
c) C,E
d) T,H
Answer: B,E

25. What will be the output of the following Java program?

1. class A
2. {
3. int i;
4. void display()
5. {
6. System.out.println(i);
7. }
8. }
9. class B extends A
10. {
11. int j;
12. void display()
13. {
14. System.out.println(j);
15. }
16. }
17. class inheritance_demo
18. {
19. public static void main(String args[])
20. {
21. B obj = new B();
22. obj.i=1;
23. obj.j=2;
24. obj.display();
25. }
26. }

a) 0
b) 1
c) 2
d) Compilation Error
Answer: 2

26. What will be the output of the following Java program?

1. class A
2. {
3. int i;
4. }
5. class B extends A
6. {
7. int j;
8. void display()
9. {
10. super.i = j + 1;
11. System.out.println(j + " " + i);
12. }
13. }
14. class inheritance
15. {
16. public static void main(String args[])
17. {
18. B obj = new B();
19. obj.i=1;
20. obj.j=2;
21. obj.display();
22. }
23. }

a) 2 2
b) 3 3
c) 2 3
d) 3 2
Answer: 2 3

27. What will be the output of the following Java program?

1. class A
2. {
3. public int i;
4. public int j;
5. A()
6. {
7. i = 1;
8. j = 2;
9. }
10. }
11. class B extends A
12. {
13. int a;
14. B()
15. {
16. super();
17. }
18. }
19. class super_use
20. {
21. public static void main(String args[])
22. {
23. B obj = new B();
24. System.out.println(obj.i + " " + obj.j)
25. }
26. }

a) 1 2
b) 2 1
c) Runtime Error
d) Compilation Error
Answer: 1 2
28. Which of these keywords is used to define interfaces in Java?
a) interface
b) Interface
c) intf
d) Intf
Answer: interface

29. Which of these can be used to fully abstract a class from its implementation?
a) Objects
b) Packages
c) Interfaces
d) None of the Mentioned
Answer: interfaces

30. Which of these access specifiers can be used for an interface?


a) Public
b) Protected
c) private
d) All of the mentioned
Answer: Public

31. Which of these keywords is used by a class to use an interface defined previously?
a) import
b) Import
c) implements
d) Implements
Answer: implements

32. Which of the following is the correct way of implementing an interface salary by
class manager?
a) class manager extends salary {}
b) class manager implements salary {}
c) class manager imports salary {}
d) none of the mentioned
Answer: class manager implements salary {}
33. Which of the following is an incorrect statement about packages?
a) Interfaces specifies what class must do but not how it does
b) Interfaces are specified public if they are to be accessed by any code in the program
c) All variables in interface are implicitly final and static
d) All variables are static and methods are public if interface is defined pubic
Answer: All variables are static and methods are public if interface is defined pubic

34. What will be the output of the following Java program?

1. interface calculate
2. {
3. void cal(int item);
4. }
5. class display implements calculate
6. {
7. int x;
8. public void cal(int item)
9. {
10. x = item * item;
11. }
12. }
13. class interfaces
14. {
15. public static void main(String args[])
16. {
17. display arr = new display;
18. arr.x = 0;
19. arr.cal(2);
20. System.out.print(arr.x);
21. }
22. }
a) 0
b) 2
c) 4
d) None of the mentioned
Answer:4

35. What will be the output of the following Java program?


1. interface calculate
2. {
3. void cal(int item);
4. }
5. class displayA implements calculate
6. {
7. int x;
8. public void cal(int item)
9. {
10. x = item * item;
11. }
12. }
13. class displayB implements calculate
14. {
15. int x;
16. public void cal(int item)
17. {
18. x = item / item;
19. }
20. }
21. class interfaces
22. {
23. public static void main(String args[])
24. {
25. displayA arr1 = new displayA;
26. displayB arr2 = new displayB;
27. arr1.x = 0;
28. arr2.x = 0;
29. arr1.cal(2);
30. arr2.cal(2);
31. System.out.print(arr1.x + " " + arr2.x);
32. }
33. }
a) 0 0
b) 2 2
c) 4 1
d) 1 4
Answer: 4 1

36. What will be the output of the following Java program?

1. interface calculate
2. {
3. int VAR = 0;
4. void cal(int item);
5. }
6. class display implements calculate
7. {
8. int x;
9. public void cal(int item)
10. {
11. if (item<2)
12. x = VAR;
13. else
14. x = item * item;
15. }
16. }
17. class interfaces
18. {
19.
20. public static void main(String args[])
21. {
22. display[] arr=new display[3];
23.
24. for(int i=0;i<3;i++)
25. arr[i]=new display();
26. arr[0].cal(0);
27. arr[1].cal(1);
28. arr[2].cal(2);
29. System.out.print(arr[0].x+" " + arr[1].x + " " + arr[2].x);
30. }
31. }
a) 0 1 2
b) 0 2 4
c) 0 0 4
d) 0 1 4
Answer: 0 0 4

37. Which of the following keywords is used for throwing exception manually?
a) finally
b) try
c) throw
d) catch
Answer : throw

38. Which of the following classes can catch all exceptions which cannot be caught?
a) RuntimeException
b) Error
c) Exception
d) ParentException
Answer: Error

39. Which of the following is a super class of all exception type classes?
a) Catchable
b) RuntimeExceptions
c) String
d) Throwable
Answer: Throwable

40. Which of the following operators is used to generate instance of an exception which
can be thrown using throw?
a) thrown
b) alloc
c) malloc
d) new
Answer: new

41. 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

Answer : option a

42 Which of the following is not a Java features?

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

Answer: option c

43. What is the return type of the hashCode() method in the Object
class?

a. Object
b. int
c. long
d. void

Answer: Option b

44. 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

Answer: Option a

44. What will be the output of the following program?

1. public class Test {


2. public static void main(String[] args) {
3. int count = 1;
4. while (count <= 15) {
5. System.out.println(count % 2 == 1 ? "***" : "+++++");
6. ++count;
7. } // end while
8. } // end main
9. }

a. 15 times ***
b. 15 times +++++
c. 8 times *** and 7 times +++++
d. Both will print only once

Answer: Option c

45.

1.Which collection class allows you to grow or shrink its size and provides indexed
access to its elements, but whose methods are not synchronized?
A. java.util.HashSet

B. java.util.LinkedHashSet

C. java.util.List

D. java.util.ArrayList
Answer: D

46. 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 )

Answer : option a

47. 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

Answer : option d

48. 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.

Answer: option d

49. n interface with no fields or methods is known as a ______.

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

Answer: option b

50. 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.

Answer: Option c

51. 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

Answer: option c

52. 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

Answer : Option b

53. Which keyword is used for accessing the features of a package?

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

Answer : Option b

54. What will be the output of the following program?

1. public class Test2 {


2. public static void main(String[] args) {
3. StringBuffer s1 = new StringBuffer("Complete");
4. s1.setCharAt(1,'i');
5. s1.setCharAt(7,'d');
6. System.out.println(s1);
7. }
8. }

a. Complete
b. Iomplede
c. Cimpletd
d. Coipletd

Answer: Option c

55. 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()

Answer: Option c

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

1. Student studentName, studentId;


2. studentName = new Student();
3. 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.

Answer: Option a
57. Which of the following is a mutable class in java?

a. java.lang.String
b. java.lang.Byte
c. java.lang.Short
d. java.lang.StringBuilder

Answer: Option d

58. Given,

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


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

Find the value of value[i]?

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

Answer: Option d

59. 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

Answer: Option b

60. 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.

Answer: Option b

61. What is the order of variables in Enum?


a) Ascending order
b) Descending order
c) Random order
d) Depends on the order() method
Answer: Option a

62. Can we create an instance of Enum outside of Enum itself?


a) True
b) False

Answer: Option b
63. What will be the output of the following Java code?

1. enum Season
2. {
3. WINTER, SPRING, SUMMER, FALL
4. };
5. System.out.println(Season.WINTER.ordinal());
a) 0
b) 1
c) 2
d) 3
Answer: Option a

64 Which class does all the Enums extend?


a) Object
b) Enums
c) Enum
d) EnumClass

Answer: option c

65. Which of these standard collection classes implements a dynamic array?


a) AbstractList
b) LinkedList
c) ArrayList
d) AbstractSet
Answer: option c

66. Which of these class can generate an array which can increase and
decrease in size automatically?
a) ArrayList()
b) DynamicList()
c) LinkedList()
d) MallocList()

Answer: option a

67. Which of these method of ArrayList class is used to obtain present size of an
object?
a) size()
b) length()
c) index()
d) capacity()

Answer : option a

68. Which of these methods can be used to obtain a static array from an
ArrayList object?
a) Array()
b) covertArray()
c) toArray()
d) covertoArray()

Answer: Option c

69. What will be the output of the following Java program?

1. import java.util.*;
2. class Arraylist
3. {
4. public static void main(String args[])
5. {
6. ArrayList obj = new ArrayList();
7. obj.add("A");
8. obj.add("B");
9. obj.add("C");
10. obj.add(1, "D");
11. System.out.println(obj);
12. }
13. }
a) [A, B, C, D]
b) [A, D, B, C]
c) [A, D, C]
d) [A, B, C]

Answer: Option b

70. What will be the output of the following Java program?

1. import java.util.*;
2. class Output
3. {
4. public static void main(String args[])
5. {
6. ArrayList obj = new ArrayList();
7. obj.add("A");
8. obj.add(0, "B");
9. System.out.println(obj.size());
10. }
11. }
a) 0
b) 1
c) 2
d) Any Garbage Value

Answer: Option c

71. Which of the following is a type of polymorphism in Java?


a) Compile time polymorphism
b) Execution time polymorphism
c) Multiple polymorphism
d) Multilevel polymorphism

Answer: Option a

72. When does method overloading is determined?


a) At run time
b) At compile time
c) At coding time
d) At execution time

Answer: option b

73. When Overloading does not occur?


a) More than one method with same name but different method signature and
different number or type of parameters
b) More than one method with same name, same signature but different number
of signature
c) More than one method with same name, same signature, same number of
parameters but different type
d) More than one method with same name, same number of parameters and
type but different signature

Answer: Option d

74. Which concept of Java is achieved by combining methods and attribute into a
class?
a) Encapsulation
b) Inheritance
c) Polymorphism
d) Abstraction

Answer : Option a

75. Method overriding is combination of inheritance and polymorphism?


a) True
b) false

Answer: Option a

76. Which of these keywords is not a part of exception handling?


a) try
b) finally
c) thrown
d) catch

Answer: Option c

77. Which of these keywords must be used to monitor for exceptions?


a) try
b) finally
c) throw
d) catch

Answer: Option a
78. Which of these keywords must be used to handle the exception thrown by try block in
some rational manner?
a) try
b) finally
c) throw
d) catch

Answer: Option d

79. Which of these keywords is used to manually throw an exception?


a) try
b) finally
c) throw
d) catch

Answer: Option c

80, What will be the output of the following Java program?

1. class exception_handling
2. {
3. public static void main(String args[])
4. {
5. try
6. {
7. System.out.print("Hello" + " " + 1 / 0);
8. }
9. catch(ArithmeticException e)
10. {
11. System.out.print("World");
12. }
13. }
14. }
a) Hello
b) World
c) HelloWorld
d) Hello World

Answer: Option b

81. What will be the output of the following Java program?

1. class exception_handling
2. {
3. public static void main(String args[])
4. {
5. try
6. {
7. int a, b;
8. b = 0;
9. a = 5 / b;
10. System.out.print("A");
11. }
12. catch(ArithmeticException e)
13. {
14. System.out.print("B");
15. }
16. }
17. }
a) A
b) B
c) Compilation Error
d) Runtime Error

Answer: Option b

82. What will be the output of the following Java program?

1. class exception_handling
2. {
3. public static void main(String args[])
4. {
5. try
6. {
7. int a, b;
8. b = 0;
9. a = 5 / b;
10. System.out.print("A");
11. }
12. catch(ArithmeticException e)
13. {
14. System.out.print("B");
15. }
16. finally
17. {
18. System.out.print("C");
19. }
20. }
21. }
a) A
b) B
c) AC
d) BC

Answer: Option d

83. What will be the output of the following Java program?


1. class exception_handling
2. {
3. public static void main(String args[])
4. {
5. try
6. {
7. int i, sum;
8. sum = 10;
9. for (i = -1; i < 3 ;++i)
10. sum = (sum / i);
11. }
12. catch(ArithmeticException e)
13. {
14. System.out.print("0");
15. }
16. System.out.print(sum);
17. }
18. }
a) 0
b) 05
c) Compilation Error
d) Runtime Error

Answer: Option c

84. 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

Answer: Option a

85. 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: Option c
86. Which of this access specifies can be used for a class so that its members
can be accessed by a different class in the same package?
a) Public
b) Protected
c) No Modifier
d) All of the mentioned

Answer: Option d

87. Which of this method can be used to make the main thread to be executed
last among all the threads?
a) stop()
b) sleep()
c) join()
d) call()

Answer: Option c

88. Which of this method is used to find out that a thread is still running or not?
a) run()
b) Alive()
c) isAlive()
d) checkRun()

Answer: Option c

89. What is the default value of priority variable MIN_PRIORITY AND


MAX_PRIORITY?
a) 0 & 256
b) 0 & 1
c) 1 & 10
d) 1 & 256

Answer: Option c

90. Which of these method waits for the thread to terminate?


a) sleep()
b) isAlive()
c) join()
d) stop()
Answer: Option c

91. Which of these method is used to explicitly set the priority of a thread?
a) set()
b) make()
c) setPriority()
d) makePriority()

Answer: Option c

92. Which of these package contains classes and interfaces for networking?
a) java.io
b) java.util
c) java.net
d) java.network

Answer: Option c

93. Which of these is a protocol for breaking and sending packets to an address across a
network?
a) TCP/IP
b) DNS
c) Socket
d) Proxy Server

Answer: Option a

94. How many ports of TCP/IP are reserved for specific protocols?
a) 10
b) 1024
c) 2048
d) 512

Answer: Option b

95. Which of these is a full form of DNS?


a) Data Network Service
b) Data Name Service
c) Domain Network Service
d) Domain Name Service
Answer: Option d

96. Which of these class is used to encapsulate IP address and DNS?


a) DatagramPacket
b) URL
c) InetAddress
d) ContentHandler

Answer: Option c

97. Which version of Java introduced annotation?


a) Java 5
b) Java 6
c) Java 7
d) Java 8

Answer: Option a

98. Annotation type definition looks similar to which of the following?


a) Method
b) Class
c) Interface
d) Field

Answer: Option c

99. Which of the following is not pre defined annotation in Java?


a) @Deprecated
b) @Overriden
c) @SafeVarags
d) @FunctionInterface

Answer: Option b

100. Which of these access specifiers can be used for an interface?


a) Public
b) Protected
c) private
d) All of the mentioned

Answer: Option a

You might also like