MCQ on JAVA: A Programming Language
42. What will be the output of following code?
@FunctionalInterface
interface Test
{
int A(int a,int
b);
static String
show()
{
return
"Hello";
}
}
public class CA{
public static void main(String args[])
{
Test
ref=(p,q)->p+q;
System.out.println(ref.A(1,2)+" "+Test.show());
}
}
- Compile
time error
- 3 Hello
- Runtime
error
- Hello
43. What will be the output of following code?
@FunctionalInterface
interface Test1
{
int A(int a);
}
@FunctionalInterface
interface Test2
{
int B(int b);
}
@FunctionalInterface
interface Test3
{
int C();
}
public class Test{
public static void main(String args[])
{
Test1
ref1=(n1)->n1*n1;
Test2
ref2=(n2)->n2*2;
Test3
ref3=()->{return ref1.A(2)+ref2.B(3);};
System.out.println(ref3.C());
}
}
- 10
- Compile
time error
- Runtime
error
- 0
44. What will be the output of following code?
@FunctionalInterface
interface Test1
{
int A(int a,int
b);
}
public class CA{
public static void main(String args[])
{
Test1
ref1=(n1,n2)->{
return
n1+2>n2-2?100:200;
};
System.out.println(ref1.A(1,5));
}
}
- 100
- 200
- Compile
time error
- Runtime
error
45. What is the process of defining more than one method in a
class differentiated by parameters?
- Method
overriding
- Method
overloading
- Method
chaining
- None
of the mentioned
46. Predict the output?
class Main
{
public static
void main(String args[])
{
int arr[] = new int[] {0 , 1, 2, 3, 4,
5, 6, 7, 8, 9};
int n = 4;
n = arr[arr[n+1] / 2];
System.out.println(arr[n+1] % 3);
}
}
a) 3
b) 0
c) 6
d) 1
47. What will be the output of the following Java code?
enum Season
{
WINTER,
SPRING, SUMMER, FALL
};
class Main
{
public static void main (String[] args)
{
System.out.println(Season.FALL.ordinal());
}
}
a) 0
b) 1
c) 2
d) 3
48. What is the output of the following code?
class Test {
public
static void main(String[] args) {
for(int
i = 0; 1; i++) {
System.out.println("Hello");
break;
}
}
}
a. Hello
b. Hello will be printed infinite time
c. Compile time error
d. None of these.
49. What will be the output of following code?
public class Test
{
public static void main(String[] args)
{
short x=45;
byte y=x;
System.out.println(y);
}
}
- 45
- 0
- Compile
time error
- -1
50. What will be the output of following code?
class Evaluation
{
void task()
{
class
Test_local
{
static
final int a=100;
int
value()
{
return
a;
}
}
Test_local
obj=new Test_local();
System.out.println(obj.value());
}
}
public class Task
{
public static void main(String[] args)
{
Evaluation
ref=new Evaluation();
ref.task();
}
}
- 100
- 0
- Compile
time error
- Runtime
error
51. What will be the output of following code?
abstract class Test
{
static int
x=1,y=2;
abstract void
show();
static int
result()
{
return x*y;
}
}
public class Task
{
public
static void main(String[] args)
{
Test
ref = new Test()
{
void
show()
{
System.out.println(Test.result());
}
};
ref.show();
}
}
- Compile
time error
- Runtime
error
- 2
- 0
52. What will be the output of following code?
interface Test
{
void show1();
void show2();
}
public class Task
{
public
static void main(String[] args)
{
Test
obj = new Test()
{
public void show1()
{
System.out.println(“Hello");
}
};
obj.show1();
}
}
- CA2
- Compile
time error
- Runtime
error
- Blank
Output
53. Output of the code
import java.util.*;
public class Main {
public
static void main(String[] args)
{
TreeSet<String>
treeSet = new TreeSet<>();
treeSet.add(“B");
treeSet.add(“A");
treeSet.add(“D");
treeSet.add(“C");
for
(String temp : treeSet)
System.out.printf(temp
+ " ");
}
}
OP1.) A B
C D
OP2.) B A
D C
OP3.) D C
B A
OP4.) None of these
54. What will be the output of following code?
public class First
{
public static void main(String[] args)
{
int a=6,b=7;
boolean c;
c=++a==b||b++>=8;
System.out.println(c+" "+b);
}
}
- true 8
- false 7
- true 7
- false 8
55. What will be the output of following code?
public class First
{
public static void main(String[] args)
{
int a=100;
boolean b=false;
System.out.println(++a>100&!b);
}
}
- true
- false
- 100
- -1
56. Which of these is the correct syntax for array creation?
a) int arr[] = new arr[5]
b) int [5] arr = new int[]
c) int arr[5] = new int[]
d) int arr[] = new int [5]
57. Output of the code
enum Flowers
{
SUNFLOWER,JASMINE,LOTUS;
}
public class Main
{
public
static void main(String[] args) {
Flowers
var[]=Flowers.values();
for(int
i=1;i<2;i++)
System.out.println(var[i]);
}
}
- JASMINE
- LOTUS
- SUNFLOWER
- 1
58. What will be the output of following code?
public class abc2
{
public static void main(String[] args)
{
long x=123;
int y=x;
System.out.println(y);
}
}
- 123
- 123000
- Compile
time error
- 0
59. Which of these keywords is used to manually throw an exception?
a) throws
b) finally
c) throw
d) catch
60. Which part of code gets executed whether exception is caught or not?
a) finally
b) try
c) catch
d) throw
61. Which of the following handles the exception when a catch is not used?
a) finally
b) throw handler
c) default handler
d) java run time system
62. What will be the output of the following Java program?
import
java.io.*;
class
filesinputoutput
{
public
static void main(String args[])
{
InputStream obj = new FileInputStream("inputoutput.java");
System.out.print(obj.available());
}
}
[Note: inputoutput.java is stored in the disk.]
a) true
b) false
c) prints number of bytes in file
d) prints number of characters in the file
63. What will be the output of the following Java code?
import
java.io.*;
class files
{
public
static void main(String args[])
{
File
obj = new File("/java/system");
System.out.print(obj.getName());
}
}
a) java
b) system
c) java/system
d) /java/system
64. What will happen when you compile and run the following code with assertion enabled?
public class Test{
public
static void main(String[] args){
int age = 20;
assert
age > 20 : getMessage();
System.out.println("Valid");
}
private
static void getMessage() {
System.out.println("Not
valid");
}
}
A. The code will
not compile
B. The code will
compile but will throw AssertionError when executed
C. The code will
compile and print Not Valid
D. The code will
compile and print Valid
65. What will happen when you compile and run the following code with assertion enabled?
public class Test{
public static void main(String[] args){
assert
false;
System.out.println("True");
}
}
A. The code will not compile due to unreachable code since
false is hard coded in assert statement
B. The code will compile but will throw AssertionError
when executed
C. The code will compile and print true
D. None of the above
66. Output of the code
public class pqr{
public
static void main(String[] args){
int
i = 10;
int
j = 24;
int
k = 34;
assert
i + j >= k : k--;
System.out.println(i
+ j + k);
}
}
- 67
- 68
- Compiles
fine but gives AssertionError
- None of these
67. What will be the output of the program?
public class Test
{
public static void
main(String[] args)
{
int x =
0;
assert (x >
0) : "assertion failed"; /* Line 6 */
System.out.println("finished");
}
}
[A] finished
[B] Compilation
fails.
[C] An
AssertionError is thrown.
[D] An
AssertionError is thrown and finished is output.
68. Output?
public class Test{
public
static void main(String[] args){
boolean
b = false;
assert
b = true;
System.out.println("Hi");
}
}
- Hi
- The
code will not compile
- The
code will compile but will throw AssertionError when executed
- None
of these
Reply with suitable and helpful comment