Question : Which action can be used to load a database driver by using JDBC.?
1. Add the driver class to the META-INF/services folder of the JAR file
2. Include the JDBC driver class in a jdbc.properties file.
3. Use the java.lang.Class.forName method to load the driver class.
4. Use the DriverManager.getDriver method to load the driver class.
Correct Answer : Get Lastest Questions and Answer : Explanation: : Driver java.sql.DriverManager.getDriver(String url) throws SQLException
@CallerSensitive Attempts to locate a driver that understands the given URL. The DriverManager attempts to select an appropriate driver from the set of registered JDBC drivers. Parameters: url a database URL of the form jdbc:subprotocol:subname Returns: a Driver object representing a driver that can connect to the given URL Throws: SQLException - if a database access error occurs
Question : You have been given below code, what is the expected behavior assume below path does not exist "/com/HadoopExam.java" ?
public static void main(String[] args) throws IOException { Path p1 = Paths.get("/com/HadoopExam.java"); System.out.println(p1.getNameCount() + ":" + p1.getName(1) + ":" + p1.getFileName()); } } 1. It will give compile time error.
2. It will throw "NullPointerException"
3. It will throe "FileNotFoundException"
4. It will print "2:HadoopExam.java:HadoopExam.java"
5. It will print "2:com:HadoopExam.java"
Correct Answer : Get Lastest Questions and Answer : Explanation: when you use Path class to represent a path. Then it is not required that path exist.
java.nio.file.Paths
This class consists exclusively of static methods that return a Path by converting a path string or URI. int java.nio.file.Path.getNameCount()
Returns the number of name elements in the path. Returns: the number of elements in the path, or 0 if this path only represents a root component
Path java.nio.file.Path.getName(int index)
Returns a name element of this path as a Path object. The index parameter is the index of the name element to return. The element that is closest to the root in the directory hierarchy has index 0. The element that is farthest from the root has index count-1. Parameters: index the index of the element
Path java.nio.file.Path.getFileName()
Returns the name of the file or directory denoted by this path as a Path object. The file name is the farthest element from the root in the directory hierarchy. Returns: a path representing the name of the file or directory, or null if this path has zero elements
Question : You have been given below code, what is the expected behavior?
package com.hadoopexam;
import java.util.concurrent.atomic.AtomicInteger;
class Welcome implements Runnable { private static AtomicInteger count = new AtomicInteger(5);
public void run() { int counterValue = count.incrementAndGet(); System.out.print(counterValue + " "); }
public static void main(String[] args) { Thread thread1 = new Thread(new Welcome()); Thread thread2 = new Thread(new Welcome()); Thread thread3 = new Thread(new Welcome()); Thread[] ta = { thread1, thread2, thread3 }; for (int x = 0; x < 3; x++) { ta[x].start(); } } }
1. It will print 5,5,5
2. It will print 5,6,7 but order can be anything
3. It will print 6,7,8
4. It will print 6,7,8 but its order can be anything
5. It will anything in any order
Correct Answer : Get Lastest Questions and Answer : Explanation: There is no predefined order by which thread can be executed. However, the count is a static variable which will be shared between all the instances of a Welcome class. So any thread can operate on this.