public static void main(String[] args) { Welcome wel = new Welcome(); wel.printValue(); } } 1. It will be having a compile time error. Because counter1 is not properly initialized.
2. It will compile and will print "Counter Value = 0"
3. It will compile and will print "Counter Value = 10"
4. It will give runtime error as it will not get value initialized for counter. And Null primitive is not correct.
Correct Answer : 2 Explanation: In this code we will have default constructor. As void printValue() is not a valid constructor but a valid method. As constructor will never have a return type. And as we know, primitive int by default will be initialized with 0.
Question : You have been given below code, what is the expected behavior?
package com.hadoopexam;
class Welcome { int distance; Welcome(int x) { this.distance = x; }
public void increSpeed(int time) { int timeTravel = time; class Car { int value = 0; public void speed() { value = distance / timeTravel; System.out.println("Velocity with new speed " + value + "kmph"); } } new Car().speed(); }
public static void main(String[] args) { Welcome wel = new Welcome (100); wel.increSpeed(60); } }
1. It will give compile time error.
2. It will give run time error
3. It will print "Velocity with new speed 1kmph"
4. It will print "Velocity with new speed 1.4kmph"
5. It will give NullPointerException Correct Answer : Exp : It is a Question of mixing Inner classes as well access outer class variable. As we know distance variable is accessible from inner class. While creating constructor of Welcome class , we assigned distance=100. In Inner class we are acceding defined value of timeTravel=60. Hence, value= 100/60 , as we know both are int value hence result would be int value. Which is 1. So it will print Velocity with new speed 1kmph
Question : You have been given below code. Please select the correct options, which applies to this code.
package com.hadoopexam;
class Parent { public void printValue() { System.out.println("It is a Parent Class"); } }
abstract class Child extends Parent { // n1 public static void main(String[] args) { Parent obj = new Parent(); obj.printValue (); // n2 } } 1. You cannot have an abstract class which can extend non abstract class. Hence, it will give compile time error.
2. Parent class printValue() method is not accessible from a child class.
3. Program will compile successfully and also run perfectly with "It is a Parent Class".
4. There will be a RuntimeError. As child class is an abstract class.
Correct Answer : 3 Explanation: Abstract class can extend a non abstract class. Ans abstract class can have a main() method, which is static.
Question : You have been given below code, what is the expected behavior?
class Welcome { public static void main(String[] args) { IntStream stream = IntStream.of(1, 2, 3); IntFunction inFu= x -> y -> x*y;//line n1 IntStream newStream = stream.map(inFu.apply(10));// line n2 newStream.forEach(System.out::print); } } 1. This code will compile and run perfectly and produce 102030
2. This code will compile, but give run time error.
3. This code will compile and run with the 302010 as output.
4. This code will be executed successfully by changing n1 line with this "IntFunction inFu = x -> y -> x*y;"
@FunctionalInterface Represents a function that accepts an int-valued argument and produces a result. This is the int-consuming primitive specialization for Function. This is a functional interface whose functional method is apply(int). Parameters: the type of the result of the function
Returns a stream consisting of the results of applying the given function to the elements of this stream. This is an intermediate operation. Parameters: mapper a non-interfering, stateless function to apply to each element Returns: the new stream
1. It will compile and run perfectly and product output as "Value of counter is 10"
2. It will compile and run perfectly and product output as "Value of counter is 0"
3. Code will not compile as no valid Welcome(int) method is defined.
4. Code will not compile because, you are trying to restrict the visibility of toString() method.
5. As we know, every class by default extends the Object class. Which has public toString() method. Hence, you cannot restrict the access modifiers for the overridden method.