Java Core-100 MCQ Questions
Java Core-100 MCQ Questions
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()
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
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
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
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
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
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
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
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
Ans: JVM
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
a) B,E
b) A,C
c) C,E
d) T,H
Answer: B,E
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
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
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
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
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
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
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
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
Answer : option a
47. Which of the following is true about the anonymous inner class?
Answer : option d
Answer: option d
a. Runnable Interface
b. Marker Interface
c. Abstract Interface
d. CharSequence Interface
Answer: option b
Answer: Option c
Answer: option c
Answer : Option b
a. package
b. import
c. extends
d. export
Answer : Option b
a. Complete
b. Iomplede
c. Cimpletd
d. Coipletd
Answer: Option c
Answer: Option c
56. Given that Student is a class, how many reference variables and
objects are created by the following code?
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,
a. 10
b. 11
c. 15
d. None of the above
Answer: Option d
Answer: Option b
Answer: Option b
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
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
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
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
Answer: Option a
Answer: option b
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
Answer: Option a
Answer: Option c
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
Answer: Option c
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
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
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
Answer: Option c
84. Which of the following option leads to the portability and security of
Java?
Answer: Option a
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
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
Answer: Option c
Answer: Option a
Answer: Option c
Answer: Option b
Answer: Option a