What will be the output of the following code snippet? class…
What will be the output of the following code snippet? class Counter { private static int count = 0; public static synchronized void increment() { count++; try { Thread.sleep(100); } catch (InterruptedException e) {} } public static int getCount() { return count; } } public static void main(String[] args) throws InterruptedException { Thread t1 = new Thread(new Runnable() { public void run() { for (int i = 0; i < 5; i++) Counter.increment(); } }); Thread t2 = new Thread(new Runnable() { public void run() { for (int i = 0; i < 5; i++) Counter.increment(); } }); t1.start(); t2.start(); t1.join(); t2.join(); System.out.println("Final count: " + Counter.getCount()); } }
Read DetailsWhat is the output if ServletA is accessed in the browser? p…
What is the output if ServletA is accessed in the browser? public class ServletA extends HttpServlet { protected void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException { PrintWriter out = res.getWriter(); out.println(“Start of ServletA”); RequestDispatcher rd = req.getRequestDispatcher(“ServletB”); rd.include(req, res); out.println(“End of ServletA”); } } public class ServletB extends HttpServlet { protected void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException { PrintWriter out = res.getWriter(); out.println(“Inside ServletB”); } }
Read DetailsWhat will be the output of the following code snippet? class…
What will be the output of the following code snippet? class Animal { void makeSound() { System.out.println(“Animal sound”); } void sleep() { System.out.println(“Animal sleeps”); } } class Dog extends Animal { @Override void makeSound() { System.out.println(“Bark”); } void fetch() { System.out.println(“Dog fetches”); } } public class Main { public static void main(String[] args) { Animal a = new Dog(); a.makeSound(); a.sleep(); } }
Read DetailsWhat does this transaction block do if the second insert fai…
What does this transaction block do if the second insert fails? Connection con = DriverManager.getConnection(url, user, pass); con.setAutoCommit(false); try { Statement stmt = con.createStatement(); stmt.executeUpdate(“INSERT INTO users(name) VALUES (‘John’)”); stmt.executeUpdate(“INSERT INTO users(email) VALUES (‘test@example.com’)”); // Fails con.commit(); } catch (SQLException e) { con.rollback(); System.out.println(“Rolled back”); }
Read Details