Premium

Oracle Advacned Java Advanced Certification Questions and Answers (Dumps and Practice Questions)



Question : package com.hadoopexam;

class Parent {}

class FirstChild extends Parent {}

class SecondChild extends Parent {}

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
}
}
 : package com.hadoopexam;
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();
}
}
 : You have been given below code, what is the expected output?
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 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 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;

class Welcome {
int counter;

Welcome() {
Welcome(10);
}

Welcome(int r) {
counter = r;
}

void printValue() {
System.out.println("Counter Value = " + counter );
}

public static void main(String[] args) {
Welcome wel = new Welcome();
wel.printValue();
}
}
 : Please select the correct statement for the given method.
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.

Correct Answer : 3
Explanation:


Related Questions


Question : You have been given below code, what behavior are you expecting ?

package com.hadoopexam;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

class Welcome {

public static void main(String[] args) {

Stream> iStr = Stream.of(Arrays.asList("1", "Amit"),
Arrays.asList("2", null), Arrays.asList("1", "Amit"));
Stream stream = iStr.flatMap((x) -> x.stream());
stream.forEach(System.out::print);
}
}


 : You have been given below code, what behavior are you expecting ?
1. It will give compile time error

2. It will give runtime error and produce NullPointer Exception

3. It will run and print "1Amit2null1Amit"

4. It will run and print "1Amit2null"



Question : Which ONE of the following statements is TRUE?


 : Which ONE of the following statements is TRUE?
1. You cannot extend a concrete class and declare that derived class abstract

2. You cannot extend an abstract class from another abstract class

3. A n abstract class must declare at least one abstract method in it

4. You can create an instance of a concrete subclass of an abstract class but
cannot create an instance of an abstract class itself



Question : You have been given below code,

package com.hadoopexam;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Stream;

class Welcome {

public static void main(String[] args) throws IOException {

Path file = Paths.get ("Welcome.java");

//n1
}
}

Which of the below code , can be replaced with line n1 so that Welcome.java contents can be printed?

 : You have been given below code,
1. Stream fc = Files.lines(file);
fc.forEach (s -> System.out.println(s));

2. Stream fc = Files.readAllLines(file);
fc.forEach (s -> System.out.println(s));


3. List fc = readAllLines(file);
fc.stream().forEach (s - > System.out.println(s));


4. List fc = Files.list(file);
fc.stream().forEach(s -> System.out.println(s));



Question : You have been given below code, which of the given snippet can be replaced at line n. So that entire file content can be printed at console?

package com.hadoopexam;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Stream;

class Welcome {

public static void main(String[] args) throws IOException {

Path file = Paths.get ("HadoopExam.txt");
//n1
}
}
 : You have been given below code, which of the given snippet can be replaced at line n. So that entire file content can be printed at console?
1. Stream fc = (Stream) Files.readAllLines(file);
fc.forEach (s -> System.out.println(s));

2. Stream fc = Files.lines (file);
fc.forEach (s -> System.out.println(s));


3. List fc = Files.readAllLines(file);
fc.stream().forEach (s -> System.out.println(s));



Question : You have following declaration, please select the one which correctly applies

package com.hadoopexam;

public abstract final class Welcome { }
 : You have following declaration, please select the one which correctly applies
1. It will throw a compile time error, as you cannot have Class without any method.

2. It will not throw any compile time error.

3. It will throw compile time error because, you can not have a final abstract class.

4. You must have at least one abstract method.



Question : You have been given below code, what is the expected behavior ?

package com.hadoopexam;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;

class Welcome {
public void fileDelete(String dirName) throws IOException {
File[] allFilesAndDir = new File(dirName).listFiles();
System.out.println(Arrays.toString(allFilesAndDir));
if (allFilesAndDir != null && allFilesAndDir.length > 0) {
for (File file : allFilesAndDir) {
if (file.isDirectory()) {
fileDelete(file.getAbsolutePath());
} else {
if (file.getName().endsWith(".class")) {
System.out.println("Deleted "+file);
file.delete();
}
}
}
}
}

public static void main(String[] args) throws IOException {
Welcome wel = new Welcome();
System.out.println(System.getProperty("user.dir"));
wel.fileDelete(System.getProperty("user.dir"));

}
}






 : You have been given below code, what is the expected behavior ?
1. The method deletes all the.class files in the Projects directory and its subdirectories.

2. The method deletes the .class files of the Projects directory only.

3. The method executes and does not make any changes to the Projects directory.

4. The method throws an IOException.