class Welcome { public static void main(String[] args) { Parent[] array = new FirstChild[3]; //n1 array[0] = new FirstChild(); //n2 array[2] = new SecondChild(); //n3 System.out.println(array.length); //n4 } } 1. It will throws an ArrayStoreException, when program is run.
2. It will throw compile time exception at line n1 and n2 both.
3. It will throw compile time exception at line n3.
4. It will run successfully and print 3
5. It will run successfully and print 2
Correct Answer : 1 Explanation: Creating an array with child class and assigning type of array with parent is fine. Hoiwever, we have created an array of FirstChild, which has parent child relation only with the Parent class. SecondChild has no relation with FirstChild. Hence, if you are try to add an SecondChild object to FirstChild it will throw Runtime exception named as ArrayStoreException.
Question : You have been given below code, what is the expected output?
package com.hadoopexam;
import java.util.Arrays; import java.util.List;
class Welcome { public static void main(String[] args) { List values = Arrays.asList(5,5,5); values.stream().map(n -> n * 2).peek(System.out::print).count(); } } 1. It will compile time error
2. It will compile perfectly but give RuntimeError
3. It will compile and run perfectly, but will not produce any output.
4. It will compile and run perfectly, but will produce 555 as output.
5. It will compile and run perfectly, but will produce 101010 as output.
Correct Answer : 5 Explanation: Stream java.util.stream.Stream.peek(Consumer super Integer> action)
Returns a stream consisting of the elements of this stream, additionally performing the provided action on each element as elements are consumed from the resulting stream. This is an intermediate operation. For parallel stream pipelines, the action may be called at whatever time and in whatever thread the element is made available by the upstream operation. If the action modifies shared state, it is responsible for providing the required synchronization. Parameters: action a non-interfering action to perform on the elements as they are consumed from the stream Returns: the new stream
long java.util.stream.Stream.count()
Returns the count of elements in this stream. This is a special case of a reduction and is equivalent to: return mapToLong(e -> 1L).sum(); This is a terminal operation. Returns: the count of elements in this stream
Stream java.util.stream.Stream.map(Function super Integer, ? extends Integer> mapper)
Returns a stream consisting of the results of applying the given function to the elements of this stream. This is an intermediate operation. Parameters: The element type of the new stream mapper a non-interfering, stateless function to apply to each element Returns: the new stream
Stream java.util.Collection.stream()
Returns a sequential Stream with this collection as its source. This method should be overridden when the spliterator() method cannot return a spliterator that is IMMUTABLE, CONCURRENT, or late-binding. (See spliterator() for details.) Returns: a sequential Stream over the elements in this collection
Question : Please select the correct statement for the given method. package com.hadoopexam;
public static void main(String[] args) { Welcome wel = new Welcome(); wel.printValue(); } } 1. It will compile and run perfectly with output as Counter Value =10.
2. It will compile perfectly, but give runtime Exception.
3. It will not even compile, because method Welcome(int ) is not available.
4. It will compile and run perfectly with output as Counter Value =0.
5. : As you know, whenever you do constructor chaining i.e. Same class constructor should be called by another constructor. You should have use this(10) instead of Welcome(10). As it is trying to look for Welcome(10) method.