In this post, I'm going to share some Java Quizzes which are asked in I.T. companies.
Quiz 1:
public class Code1 extends Code2{
public static void main(){
new Code1();
new Code1("Code1");
}
Code1(){
this("Works");
}
public class Code2{
Code2(String str){
System.out.println(str);
}
}
O/P: WorksCode1
Explanation: Default constructor always calls super constructor.
Quiz 2:
public class Math{
static int i = 10;
static{
i = j+5;
}
static int j = 3;
public static void sum(int number){
System.out.println("" + (number+number));
}
O/P: It will result in compilation error.
Explanation:
Because the static block and variables follow the order in which they appear. The variable j is not available to the static block.
Quiz 3:
O/P: Compilation error
Explanation:
Compiler will not be able to resolve which of the overloaded methods to invoke.
Note: If we take only one method above, then it will get called. These are called varargs method.
But remember, if we take an array instead of varargs, then it shows compile time error. It will not call the method containing array as argument type.
Quiz 4:
public class MultipleMethods{
public static int getValue(Integer integer){
return 1;
}
public static void main(String[] args){
for(String i : args){
System.out.print(i);
}
System.out.print(8);
}
}
Quiz 1:
public class Code1 extends Code2{
public static void main(){
new Code1();
new Code1("Code1");
}
Code1(){
this("Works");
}
Code1(String str){
super(str);
}
}public class Code2{
Code2(String str){
System.out.println(str);
}
}
O/P: WorksCode1
Explanation: Default constructor always calls super constructor.
Quiz 2:
public class Math{
static int i = 10;
static{
i = j+5;
}
static int j = 3;
public static void sum(int number){
System.out.println("" + (number+number));
}
public static void main(String[] args){
sum(i);
}
O/P: It will result in compilation error.
Explanation:
Because the static block and variables follow the order in which they appear. The variable j is not available to the static block.
Quiz 3:
public static void sum(float… v){
System.out.println("float");
}
public static void sum(boolean… v){
System.out.println("boolean");
}
public static void main(String[] args){
sum();
}
O/P: Compilation error
Explanation:
Compiler will not be able to resolve which of the overloaded methods to invoke.
Note: If we take only one method above, then it will get called. These are called varargs method.
But remember, if we take an array instead of varargs, then it shows compile time error. It will not call the method containing array as argument type.
Quiz 4:
public class MultipleMethods{
public static int getValue(Integer integer){
return 1;
}
public static void main(String[] args){
for(String i : args){
System.out.print(i);
}
System.out.print(8);
}
}
public class NewMethods{
public static void main(String[] args){
String[] numbers = {"1", "2","3"};
MultipleMethods.main(numbers);
}
}
O/P: 1 2 3 8
Explanation:
We can call main method of a class from another class.
Quiz 5:
public class ObjectValue{
public static String getValues(Integer values){
return "Objects";
}
}
public class IntValue extends ObjectValue{
public static String getValues(Integer values) throws Exception{
return "Integers";
}
}
public class Main{
public static void main(String[] args){
ObjectValue ov = new IntValue();
try{
System.out.println(ov.getValues(null));
}
catch(Exception e ){
}
}
}
O/P: Compile-time error.
Explanation:
Overriding methods in the derived class cannot throw newer or broader exceptions. But it can throw narrow exception. e.g. if Parent class throws Exception, then derived class can throw Exception or any subclass of Exception.
Quiz 6:
public static String getValue(Integer val){
return "Integer";
}
public static String getValue(Object obj){
return "Object";
}
public static void main(){
System.out.println(getValue(null));
}
O/P: Integer
Explanation:
For method overloading, most specific method is chosen over generic option.
Quiz 7:
int start = Integer.MAX_VALUE- 10;
int end = Integer.MAX_VALUE;
int counter = 0;
for(int j = start; j<=end; j++){
System.out.println("j = "+j);
counter++;
}
System.out.println(counter);
O/P: It prints large number of int values.
Explanation:
Integers are not same as int.
Quiz 8:
System.out.println("H" + 'I'); // prints HI, as String concatenation works here
System.out.println('H' + "I"); // prints HI, as String concatenation works here
System.out.println('H' + 'I'); // Prints 145
System.out.println('H'); // Prints H
System.out.println('I'); // Prints I
Quiz 9:
String strName = "Javafreeway";
Runnable r = () -> {
System.out.print(strName);
strName = strName.toUpperCase();
};
r.run();
O/P: Compilation error.
Explanation:
Local variables referenced from a lambda expression must be final or effectively final.
Quiz 6:
public static String getValue(Integer val){
return "Integer";
}
public static String getValue(Object obj){
return "Object";
}
public static void main(){
System.out.println(getValue(null));
}
O/P: Integer
Explanation:
For method overloading, most specific method is chosen over generic option.
Quiz 7:
int start = Integer.MAX_VALUE- 10;
int end = Integer.MAX_VALUE;
int counter = 0;
for(int j = start; j<=end; j++){
System.out.println("j = "+j);
counter++;
}
System.out.println(counter);
O/P: It prints large number of int values.
Explanation:
Integers are not same as int.
Quiz 8:
System.out.println("H" + 'I'); // prints HI, as String concatenation works here
System.out.println('H' + "I"); // prints HI, as String concatenation works here
System.out.println('H' + 'I'); // Prints 145
System.out.println('H'); // Prints H
System.out.println('I'); // Prints I
Quiz 9:
String strName = "Javafreeway";
Runnable r = () -> {
System.out.print(strName);
strName = strName.toUpperCase();
};
r.run();
O/P: Compilation error.
Explanation:
Local variables referenced from a lambda expression must be final or effectively final.
Quiz 10:
class Code1{
public static String getMessage(){
return "HelloA--";
}
class Code1{
public static String getMessage(){
return "HelloA--";
}
public static String getName(){
return "C1";
}
}
class Code2 extends Code1{
public static String getMessage(){
return "Hello";
}
public String getName(){
return "C2";
}
}
public class Overriding{
public static void main(String[] args){
Code1 c1 = new Code2();
System.out.println(c1.getMessage() + " " + c1.getName());
}
}
O/P: HelloA-- C2
Explanation:
static methods are called based on class reference.
If this post really helped you, Kindly hit Like on this post.
Thanks!
If this post really helped you, Kindly hit Like on this post.
Thanks!
No comments:
Post a Comment