Java MCQ Questions and answers. Important basic to advance MCQs for freshers interview placement and Competitive Exam’s preparation with PDF Download. Helpful for TCS, Wipro, COPA, Virtusa, DCA, Wings, infytq and other exam.
Java MCQ Questions and answers
1. Number of primitive data types in Java are?
a) 6
b) 7
c) 8
d) 9
Ans. c
2. What is the size of float and double in Java?
a) 32 and 64
b) 32 and 32
c) 64 and 64
d) 64 and 32
Ans. a
3. Automatic type conversion is possible in which of the possible cases?
a) Byte to int
b) Int to long
c) Long to int
d) Short to int
Ans. b
4. Find the output of the following code.
int Integer = 24;
char String = ‘I’;
System.out.print(Integer);
System.out.print(String);
a) Compile error
b) Throws exception
c) I
d) 24 I
Ans. d
5. Select the valid statement.
a) char[] ch = new char(5)
b) char[] ch = new char[5]
c) char[] ch = new char()
d) char[] ch = new char[]
Ans. b
6. When an array is passed to a method, what does the method receive?
a) The reference of the array
b) A copy of the array
c) Length of the array
d) Copy of the first element
Ans. a
7. Select the valid statement to declare and initialize an array.
a) int[] A = {}
b) int[] A = {1, 2, 3}
c) int[] A = (1, 2, 3)
d) int[][] A = {1, 2, 3}
Ans. b
8. Arrays in java are-
a) Object references
b) objects
c) Primitive data type
d) None
Ans. b
9. When is the object created with new keyword?
a) At run time
b) At compile time
c) Depends on the code
d) None
Ans. a
10. Identify the correct definition of a package.
a) A package is a collection of editing tools
b) A package is a collection of classes
c) A package is a collection of classes and interfaces
d) A package is a collection of interfaces
Ans. c
11. Identify the correct restriction on static methods.
i) They must access only static data
ii) They can only call other static methods
iii) They cannot refer to this or super
a) I and ii
b) iiand iii
c) Only iii
d) I, ii and iii
Ans. d
12. Identify the keyword among the following that makes a variable belong to a class, rather than being defined for each instance of the class.
a) final
b) static
c) volatile
d) abstract
Ans. b
13. In which of the following is toString() method defined?
a) java.lang.Object
b) java.lang.String
c) java.lang.util
d) None
Ans. a
14. CompareTo() returns
a) True
b) False
c) An int value
d) None
Ans. c
15. Identify the output of the following program.
String str = “Hellow”;
System.out.println(str.indexOf(‘t));
a) 0
b) 1
c) True
d) -1
Ans. d (Since, t isn’t present in the string str, it returns -1.)
16. To which of the following does the class string belong to.
a) java.lang
b) java.awt
c) java.applet
d) java.string
Ans. a
17. How many objects will be created in the following?
String a = new String(“heetson”);
String b = new String(“heetson”);
Strinc c = “heetson”;
String d = “heetson”;
a) 2
b) 3
c) 4
d) None
Ans. b
Explanation: Using the new keyword creates an object everytime. Hence, 2 objects are created for first two statement. Next, a string is declared which creates another object. For the fourth statement, since, a string ”heetson” already exists, it doesn’t create an additional object again. Hence, answer is 3.
18. Total constructor string class have?
a) 3
b) 7
c) 13
d) 20
Ans. c
19. Find the output of the following code.
int ++a = 100;
System.out.println(++a);
a) 101
b) Compile error as ++a is not valid identifier
c) 100
d) None
Ans. b
20. Identify the return type of a method that does not return any value.
a) int
b) void
c) double
d) None
Ans. b
21. Output of Math.floor(3.6) is
a) 3
b) 3.0
c) 4
d) 4.0
Ans. b (floor returns largest integer that is less than or equal to the given number.)
22. Where does the system stores parameters and local variables whenever a method is invoked?
a) Heap
b) Stack
c) Array
d) Tree
Ans. b
23. Identify the modifier which cannot be used for constructor.
a) public
b) protected
c) private
d) static
Ans. d
24. What is the variable declared in a class for the use of all methods of the class called?
a) Object
b) Instance variables
c) Reference variable
d) None
Ans. b
25. What is the implicit return type of constructor?
a) No return type
b) A class object in which it is defined
c) void
d) None
Ans. b
26. When is the finalize() method called?
a) Before garbage collection
b) Before an object goes out of scope
c) Before a variable goes out of scope
d) None
Ans. a
27. Identify the prototype of the default constructor.
Public class Solution {}
a) Solution(void)
b) Solution()
c) public Solution(void)
d) public Solution()
Ans. d
28. Identify the correct way of declaring constructor.
Public class Solution {}
a) Solution(){}
b) public Solution(){}
c) Solution(void){}
d) Both A and B
Ans. d
29. Identify the infinite loop.
a) for(; 😉
b) for(int i=0; i<1; i–)
c) for(int i=0; ;i++)
d) All of the above
Ans. d
30. What is Runnable?
a) Abstract class
b) Interface
c) Class
d) Method
Ans. b
31. Exception created by try block is caught in which block
a) catch
b) throw
c) final
d) none
Ans. a
32. Which of the following exception is thrown when divided by zero statement is executed?
a) NullPointerException
b) NumberFormatException
c) ArithmeticException
d) None
Ans. c
33. Where is System class defined?
a) java.lang.package
b) java.util.package
c) java.io.package
d) None
Ans. a
34. Identify the interface which is used to declare core methods in java?
a) Comparator
b) EventListener
c) Set
d) Collection
Ans. d
35. Which of the following statement are true about finalize() method?
a) It can be called Zero or one times
b) It can be called Zero or more times
c) It can be called Exactly once
d) It can be called one or more times
Ans. a
36. What does the operator >>>> do?
a) Right shift operator
b) Left shift operator
c) Zero fill left shift
d) Zero fill right sift
Ans. d
37. Identify the incorrect java feature.
a) Object oriented
b) Use of pointers
c) Dynamic
d) Architectural neural
Ans. b
38. Which environment variable is used to set the java path?
a) MAVEN_Path
b) JavaPATH
c) JAVA
d) JAVA_HOME
Ans. d
39. Which of the following is not an OOPS concept in Java?
a) Polymorphism
b) Inheritance
c) Compilation
d) Encapsulation
Ans. c
40. What is not the use of “this” keyword in Java?
a) Referring to the instance variable when a local variable has the same name
b) Passing itself to the method of the same class
c) Passing itself to another method
d) Calling another constructor in constructor chaining
Ans. b