Java Questios
Java Questios
1. class array_output
2. {
3. public static void main(String args[])
4. {
5. int array_variable [] = new int[10];
6. for (int i = 0; i < 10; ++i) {
7. array_variable[i] = i/2;
8. array_variable[i]++;
9. System.out.print(array_variable[i] + " ");
10. i++;
11. }
12.
13. }
14. }
a) 0 2 4 6 8
b) 1 2 3 4 5
c) 0 1 2 3 4 5 6 7 8 9
d) 1 2 3 4 5 6 7 8 9 10
Ans:B
13. Which of these is an incorrect array declaration?
a) int arr[] = new int[5]
b) int [] arr = new int[5]
c) int arr[] = new int[5]
d) int arr[] = int [5] new
Ans:D
14. Which of these is an incorrect Statement?
a) It is necessary to use new operator to initialize an array
b) Array can be initialized using comma separated expressions surrounded by curly
braces
c) Array can be initialized when they are declared
d) None of the mentioned
Ans:A
15. Which of these is necessary to specify at time of array initialization?
a) Row
b) Column
c) Both Row and Column
d) None of the mentioned
Ans:A
16. What will be the output of the following Java code?
1. class evaluate
2. {
3. public static void main(String args[])
4. {
5. int arr[] = new int[] {0 , 1, 2, 3, 4, 5, 6, 7, 8, 9};
6. int n = 6;
7. n = arr[arr[n] / 2];
8. System.out.println(arr[n] / 2);
9. }
10. }
a) 3
b) 0
c) 6
d) 1
Ans:D
17. What will be the output of the following Java code?
1. class array_output
2. {
3. public static void main(String args[])
4. {
5. char array_variable [] = new char[10];
6. for (int i = 0; i < 10; ++i)
7. {
8. array_variable[i] = 'i';
9. System.out.print(array_variable[i] + "");
10. }
11. }
12. }
a) 1 2 3 4 5 6 7 8 9 10
b) 0 1 2 3 4 5 6 7 8 9 10
c) i j k l m n o p q r
d) i i i i i i i i i i
Ans:D
18. Generics does not work with?
a) Set
b) List
c) Tree
d) Array
Ans:D
19. How to sort an array?
a) Array.sort()
b) Arrays.sort()
c) Collection.sort()
d) System.sort()
Ans:B
20. Which of the following can be operands of arithmetic operators?
a) Numeric
b) Boolean
c) Characters
d) Both Numeric & Characters
Ans:D
21. Modulus operator, %, can be applied to which of these?
a) Integers
b) Floating – point numbers
c) Both Integers and floating – point numbers
d) None of the mentioned
Ans:C
22. With x = 0, which of the following are legal lines of Java code for changing the value
of x to 1?
1. x++;
2. x = x + 1;
3. x += 1;
4. x =+ 1;
a) 1, 2 & 3
b) 1 & 4
c) 1, 2, 3 & 4
d) 3 & 2
Ans:C
23. Which of these statements are incorrect?
a) Assignment operators are more efficiently implemented by Java run-time system than
their equivalent long forms
b) Assignment operators run faster than their equivalent long forms
c) Assignment operators can be used only with numeric and character data type
d) None of the mentioned
Ans:D
24. What will be the output of the following Java program?
1. class increment
2. {
3. public static void main(String args[])
4. {
5. double var1 = 1 + 5;
6. double var2 = var1 / 4;
7. int var3 = 1 + 5;
8. int var4 = var3 / 4;
9. System.out.print(var2 + " " + var4);
10.
11. }
12. }
a) 1 1
b) 0 1
c) 1.5 1
d) 1.5 1.0
Ans:C
25. 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 = 1;
6. int b = 2;
7. int c;
8. int d;
9. c = ++b;
10. d = a++;
11. c++;
12. b++;
13. ++a;
14. System.out.println(a + " " + b + " " + c);
15. }
16. }
a) 3 2 4
b) 3 2 3
c) 2 3 4
d) 3 4 4
Ans:D
26. . Which of these is not a bitwise operator?
a) &
b) &=
c) |=
d) <=
Ans:D
27. What will be the output of the following Java program?
1. class bitwise_operator
2. {
3. public static void main(String args[])
4. {
5. int a = 3;
6. int b = 6;
7. int c = a | b;
8. int d = a & b;
9. System.out.println(c + " " + d);
10. }
11. }
a)7 2
b) 7 7
c) 7 5
d) 5 2
Ans:A
28. 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 = 1;
6. int b = 2;
7. int c = 3;
8. a |= 4;
9. b >>= 1;
10. c <<= 1;
11. a ^= c;
12. System.out.println(a + " " + b + " " + c);
13. }
14. }
a)3 1 6
b) 2 2 3
c) 2 3 4
d) 3 3 6
Ans:A
29. Which of these have highest precedence?
a) ()
b) ++
c) *
d) >>
Ans:A
30. Which of these statements are incorrect?
a) Equal to operator has least precedence
b) Brackets () have highest precedence
c) Division operator, /, has higher precedence than multiplication operator
d) Addition operator, +, and subtraction operator have equal precedence
Ans:C
31. Which of these selection statements test only for equality?
a) if
b) switch
c) if & switch
d) none of the mentioned
Ans:B
32. Which of these are selection statements in Java?
a) if()
b) for()
c) continue
d) break
Ans:A
33. 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:A
34. 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:D
35. 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:B
36. 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
37. Which of the following is used with the switch statement?
a) Continue
b) Exit
c) break
d) do
Ans:C
38. What is the valid data type for variable “a” to print “Hello World”?
1.switch(a)
2.{
3. System.out.println("Hello World");
4.}
a) int and float
b) byte and short
c) char and long
d) byte and char
Ans:D
39. Which of the following is not a decision making statement?
a) if
b) if-else
c) switch
d) do-while
Ans:D
40. Which of the following is not OOPS concept in Java?
a) Inheritance
b) Encapsulation
c) Polymorphism
d) Compilation
Ans:D
41. 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
Ans:A
42. Which component is used to compile, debug and execute java program?
a) JVM
b) JDK
c) JIT
d) JRE
Ans:B’
43. Which component is responsible for converting bytecode into machine specific
code?
a) JVM
b) JDK
c) JIT
d) JRE
Ans:A
44. Which component is responsible to run java program?
a) JVM
b) JDK
c) JIT
d) JRE
Ans:D
45. Which component is responsible to optimize bytecode to machine code?
a) JVM
b) JDK
c) JIT
d) JRE
Ans:C
46. Which statement is true about java?
a) Platform independent programming language
b) Platform dependent programming language
c) Code dependent programming language
d) Sequence dependent programming language
Ans:A
47. Which of the below is invalid identifier with the main method?
a) public
b) static
c) private
d) final
Ans:C
48. What is the extension of java code files?
a) .class
b) .java
c) .txt
d) .js
Ans:B
49. What is the extension of compiled java classes?
a) .class
b) .java
c) .txt
d) .js
Ans:A
50. How can we identify whether a compilation unit is class or interface from a .class
file?
a) Java source file header
b) Extension of compilation unit
c) We cannot differentiate between class and interface
d) The class or interface name should be postfixed with unit type
Ans:A
51. What is use of interpreter?
a) They convert bytecode to machine language code
b) They read high level code and execute them
c) They are intermediated between JIT and JVM
d) It is a synonym for JIT
Ans:B
52. Which of these keywords is used to make a class?
a) class
b) struct
c) int
d) none of the mentioned
Ans:A
53. Which of these statement is incorrect?
a) Every class must contain a main() method
b) Applets do not require a main() method at all
c) There can be only one main() method in a program
d) main() method must be made public
Ans:A
54 . Which of the following statements is correct?
a) Public method is accessible to all other classes in the hierarchy
b) Public method is accessible only to subclasses of its parent class
c) Public method can only be called by object of its class
d) Public method can be accessed by calling object of the public class
Ans:A
55. What will be the output of the following Java program?
1. class box
2. {
3. int width;
4. int height;
5. int length;
6. }
7. class mainclass
8. {
9. public static void main(String args[])
10. {
11. box obj = new box();
12. System.out.println(obj);
13. }
14. }
a) 0
b) 1
c) Runtime error
d) classname@hashcode in hexadecimal form
Ans:D
56. What is the return type of a method that does not return any value?
a) int
b) float
c) void
d) double
Ans:C
57. What is the process of defining more than one method in a class differentiated by
method signature?
a) Function overriding
b) Function overloading
c) Function doubling
d) None of the mentioned
Ans:B
58. Which method can be defined only once in a program?
a) main method
b) finalize method
c) static method
d) private method
Asn:A
59. Which of this statement is incorrect?
a) All object of a class are allotted memory for the all the variables defined in the class
b) If a function is defined public it can be accessed by object of other class by
inheritation
c) main() method must be made public
d) All object of a class are allotted memory for the methods defined in the class
Ans:D
60. In the following Java code, which call to sum() method is appropriate?
1.class Output
2.{
3.
4. public static int sum(int ...x)
5. {
6. return;
7. }
8. static void main(String args[])
9. {
10. sum(10);
11. sum(10,20);
12. sum(10,20,30);
13. sum(10,20,30,40);
14. }
15. }
a) only sum(10)
b) only sum(10,20)
c) only sum(10) & sum(10,20)
d) all of the mentioned
61. Garbage Collection can be controlled by a program?
a) True
b) False
Ans:B
62. What will be the output of the following Java code?
1.class San
2.{
3. public void m1 (int i,float f)
4. {
5. System.out.println(" int float method");
6. }
7.
8. public void m1(float f,int i);
9. {
10. System.out.println("float int method");
11. }
12.
13. public static void main(String[]args)
14. {
15. San s=new San();
16. s.m1(20,20);
17. }
18. }
a)int float method
b) float int method
c) compile time error
d) run time error
Ans:C
63. What will be the output of the following Java code?
1. class test
2. {
3. int a;
4. int b;
5. test(int i, int j)
6. {
7. a = i;
8. b = j;
9. }
10. void meth(test o)
11. {
12. o.a *= 2;
13. O.b /= 2;
14. }
15. }
16. class Output
17. {
18. public static void main(String args[])
19. {
20. test obj = new test(10 , 20);
21. obj.meth(obj);
22. System.out.println(obj.a + " " + obj.b);
23. }
24. }
a) 10 20
b) 20 10
c) 20 40
d) 40 20
Ans:B
64. Arrays in Java are implemented as?
a) class
b) object
c) variable
d) none of the mentioned
Ans:B
65. String in Java is a?
a) class
b) object
c) variable
d) character array
Ans:A
66. Which of these method of String class is used to obtain character at specified index?
a) char()
b) Charat()
c) charat()
d) charAt()
Ans:D
67. What will be the output of the following Java program?
1. class string_demo
2. {
3. public static void main(String args[])
4. {
5. String obj = "I" + "like" + "Java";
6. System.out.println(obj);
7. }
8. }
a) I
b) like
c) Java
d) IlikeJava
Ans:D
68. What will be the output of the following Java program?
1. class string_class
2. {
3. public static void main(String args[])
4. {
5. String obj = "I LIKE JAVA";
6. System.out.println(obj.charAt(3));
7. }
8. }
a) I
b) L
c) K
d) E
Ans:A
69. What will be the output of the following Java program?
1. class string_class
2. {
3. public static void main(String args[])
4. {
5. String obj = "I LIKE JAVA";
6. System.out.println(obj.length());
7. }
8. }
a) 9
b) 10
c) 11
d) 12
Ans:C
70. What will be the output of the following Java program?
1. class Output
2. {
3. static void main(String args[])
4. {
5. int x , y = 1;
6. x = 10;
7. if(x != 10 && x / 0 == 0)
8. System.out.println(y);
9. else
10. System.out.println(++y);
11. }
12. }
a) 1
b) 2
c) Runtime Error
d) Compilation Error
Ans:D
71. Which of these packages contains abstract keyword?
a) java.lang
b) java.util
c) java.io
d) java.system
Ans:A
72. Which of this keyword must be used to inherit a class?
a) super
b) this
c) extent
d) extends
Ans:D
73. 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 {}
Ans:C
74. What is not type of inheritance?
a) Single inheritance
b) Double inheritance
c) Hierarchical inheritance
d) Multiple inheritance
Ans:B
75. Using which of the following, multiple inheritance in Java can be implemented?
a) Interfaces
b) Multithreading
c) Protected methods
d) Private methods
Ans:A
76. All classes in Java are inherited from which class?
a) java.lang.class
b) java.class.inherited
c) java.class.object
d) java.lang.Object
Ans:D
77. Which of the following is used for implementing inheritance through class?
a) inherited
b) using
c) extends
d) implements
Ans:C