Premium

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



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));


Correct Answer : 3
Explanation: List java.nio.file.Files.readAllLines(Path path) throws IOException

Read all lines from a file. Bytes from the file are decoded into characters using the UTF-8 charset.
This method works as if invoking it were equivalent to evaluating the expression:
Files.readAllLines(path, StandardCharsets.UTF_8)
Parameters:
path the path to the file
Returns:
the lines from the file as a List; whether the List is modifiable or not is implementation dependent and therefore not specified
Throws:
IOException - if an I/O error occurs reading from the file or a malformed or unmappable byte sequence is read
SecurityException - In the case of the default provider, and a security manager is installed, the checkRead method is invoked to check read access to the file.





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.


Correct Answer : 3
Explanation: As you know, the purpose of abstract class is that, it should be extended by child classes. If you mark your abstract class as an final,
then it cannot be extended.




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.






Correct Answer : 1
Explanation: listFiles() : It will return all the paths under the Project, including file path.

As we know, here fileDelete, method is recursively called for each directory. Hence, all the .class files in directory and its subdirectory will be deleted.

File[] java.io.File.listFiles()

Returns an array of abstract pathnames denoting the files in the directory denoted by this abstract pathname.
If this abstract pathname does not denote a directory, then this method returns null. Otherwise an array of File objects is returned, one for each file or directory in the directory.
Pathnames denoting the directory itself and the directory's parent directory are not included in the result. Each resulting abstract pathname is constructed from this abstract
pathname using the File(File, String) constructor. Therefore if this pathname is absolute then each resulting pathname is absolute; if this pathname is relative then each resulting
pathname will be relative to the same directory.
There is no guarantee that the name strings in the resulting array will appear in any specific order; they are not, in particular, guaranteed to appear in alphabetical order.

boolean java.io.File.delete()

Deletes the file or directory denoted by this abstract pathname. If this pathname denotes a directory, then the directory must be empty in order to be deleted.
Note that the java.nio.file.Files class defines the delete method to throw an IOException when a file cannot be deleted. This is useful for error reporting and to diagnose why a
file cannot be deleted.
Returns:
true if and only if the file or directory is successfully deleted; false otherwise
Throws:
SecurityException - If a security manager exists and its java.lang.SecurityManager.checkDelete method denies delete access to the file


Related Questions


Question : You have been given below code.
package com.hadoopexam;

import java.io.IOException;
import java.io.ObjectInputStream.GetField;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

class Welcome {

String fName;
String lName;

public Welcome(String fn, String ln) {
fName = fn;
lName = ln;
}

public String getfName() {
return fName;
}

public String getlName() {
return lName;
}

public static void main(String[] args) throws IOException {
List allUser = Arrays.asList (
new Welcome ("Amit", "Kumar"),
new Welcome ("Amit", "Jain"),
new Welcome ("Vineet", "Sinha"));
List sortedUser = allUser.stream()
//n1

.collect(Collectors.toList());
sortedUser.stream().forEach((wel) -> System.out.println(wel.getfName() + " " + wel.getlName()));
}
}

Which of the below code will be replaced at n1, will print following result?

Vineet Sinha
Amit Jain
Amit Kumar


 : You have been given below code.
1. .sorted (Comparator.comparing(Welcome::getfName).reversed().thenComparing(Welcome::getlName))

2. .sorted (Comparator.comparing(Welcome::getfName).thenComparing(Welcome::getlName))

3. Access Mostly Uused Products by 50000+ Subscribers

4. map(Welcome::getfName).sorted(Comparator.reverseOrder().map(Welcome::getlName).reserved



Question : You have been given below code

package com.hadoopexam;

public enum INR {
RS1(100), QUARTERRS(25);
private int value;

public INR(int value) {
this.value = value;
}

public int getValue() {
return value;
}
}


package com.hadoopexam;

public class Coin {

public static void main(String[] args) {
INR inr = new INR.QUARTERRS;
System.out.println(inr.getValue());
}
}

There seems to be a compile time error. How, will fix it?

A. Remove new keyword from this line INR inr = new INR.QUARTERRS;
B. Correct statement as INR inr = new INR.QUARTERRS();
C. Keep INR class as subclass of Coin class.
D. Change the access modifier public to private for INR(int value)
 : You have been given below code
1. A,B
2. B,C
3. Access Mostly Uused Products by 50000+ Subscribers
4. A,D
5. B,D


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

package com.hadoopexam;

class PDFScanner implements AutoCloseable {
public void close() throws Exception {
System.out.print("3 Scanner closed, ");
}

public void scanPDF() throws Exception {
System.out.print("1 PDF Scanner , ");
throw new Exception("2 Unable to scan, ");
}
}

package com.hadoopexam;

class PDFPrinter implements AutoCloseable {
public void close() throws Exception {
System.out.print("5 Printer closed, ");
}

public void printCoursePDF() {
System.out.print("4 Print pdf, ");
}
}


package com.hadoopexam;

import java.io.IOException;

class Welcome {

public static void main(String[] args) throws IOException {
try (PDFScanner courseScanner = new PDFScanner(); PDFPrinter taskPrinter = new PDFPrinter()) {
courseScanner.scanPDF();
taskPrinter.printCoursePDF();
} catch (Exception e) {
System.out.print(e.getMessage());
}
}
}

 : You have been given below code, what is expected behavior?
1. 1 PDF Scanner , 5 Printer closed, 3 Scanner closed, 2 Unable to scan,

2. 5 Printer closed, 3 Scanner closed,

3. Access Mostly Uused Products by 50000+ Subscribers

4. 3 Scanner closed


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

package com.hadoopexam;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class Welcome {
static Connection newConnection = null;

public static Connection getDBConnection() throws SQLException {
try (Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "scott", "tiger")) {
newConnection = con;
}
return newConnection;
}

public static void main(String[] args) throws SQLException {
getDBConnection();
Statement st = newConnection.createStatement();
st.executeUpdate("INSERT INTO HADOOPEXAMUSER VALUES (102, 'Thomas')");
}
}

 : You have been given below code, what is the behavior expected?
1. The program executes successfully and the HADOOPEXAMUSER table is updated with one record.

2. The program executes successfully and the HADOOPEXAMUSER table is NOT updated with any record.

3. Access Mostly Uused Products by 50000+ Subscribers

4. A NullPointerExceptionis thrown as runtime.



Question : You have been given below code, what will be printed when executed?

package com.hadoopexam;

import java.util.Optional;

public class Welcome {
Optional course;

Welcome(Optional course) {
this.course = course;
}

public Optional getCourse() {
return course;
}

public static void main(String[] args) {
Course address = null;
Optional addrs1 = Optional.ofNullable(address);
Welcome e1 = new Welcome(addrs1);
String eAddress = (addrs1.isPresent()) ? addrs1.get().getName() : "Course is not subscribed";
System.out.println(eAddress);
}
}

class Course {
String name = "Hadoop";

public String getName() {
return name;
}

public String toString() {
return name;
}

}


 : You have been given below code, what will be printed when executed?
1. Hadoop

2. Course is not subscribed

3. Access Mostly Uused Products by 50000+ Subscribers

4. A NoSuchElementExceptionis thrown at run time.



Question : You have been given below source code. Select the correct behavior of it

package com.hadoopexam;

import java.util.function.Predicate;

public class Welcome {
public static void main(String[] args) {
Predicate predicate = ((Predicate) (arg -> arg == null)).negate();
System.out.println(predicate.test(null));
}
}

 : You have been given below source code. Select the correct behavior of it
1. It will give compile time error.

2. Program will compile but give run time error.

3. Access Mostly Uused Products by 50000+ Subscribers

4. Program will run perfectly and print false

5. Program will through NullPointerException at run time.