Newgen Previous Years Solved Sample Placement Papers
- What will be the output of the following Java program?
import java.net.*; class networking { public static void main(String[] args) throws Exception { URL obj = new URL("https://www.sanfoundry.com/javamcq"); URLConnection obj1 = obj.openConnection(); int len = obj1.getContentLength(); System.out.print(len); } }
Answer: b) 127 Explanation: The length of the content returned by the host URL is 127. - Which of the below is not a Java Profiler?
a) JProfiler b) Eclipse Profiler c) JVM d) JConsole
Answer: c) JVM Explanation: JVM is not a profiler. JProfiler, Eclipse Profiler, and JConsole are used to analyze and profile Java applications. - What will be the output of the following Java program?
import java.net.*; class networking { public static void main(String[] args) throws MalformedURLException { URL obj = new URL("https://www.sanfoundry.com/javamcq"); System.out.print(obj.toExternalForm()); } }
Answer: b) https://www.sanfoundry.com/javamcq Explanation: ThetoExternalForm()
method returns the full URL as a string, including the protocol. - What will be the output of the following Java code snippet?
import java.util.*; class Arraylists { public static void main(String args[]) { ArrayLists obj = new ArrayLists(); obj.add("A"); obj.add("B"); obj.add("C"); obj.add(1, "D"); System.out.println(obj); } }
Answer: d) [A, D, B, C] Explanation: The element "D" is inserted at index 1, shifting "B" and "C" one position forward. - Which of these packages contains the exception Stack Overflow in Java?
a) java.io b) java.system c) java.lang d) java.util
Answer: c) java.lang Explanation: StackOverflowError is a part of the java.lang package. - What will be the output of the following Java program?
import java.util.*; class Collection_iterators { public static void main(String args[]) { LinkedList list = new LinkedList(); list.add(new Integer(2)); list.add(new Integer(8)); list.add(new Integer(5)); list.add(new Integer(1)); Iterator i = list.iterator(); Collections.reverse(list); Collections.sort(list); while(i.hasNext()) System.out.print(i.next() + " "); } }
Answer: a) 1 2 5 8 Explanation: TheCollections.sort()
method sorts the list, andCollections.reverse()
reverses it before sorting, giving the final order: 1, 2, 5, 8. - Which of these statements is incorrect about Thread?
a) start() method is used to begin execution of the thread b) run() method is used to begin execution of a thread before start() method in special cases c) A thread can be formed by implementing Runnable interface only d) A thread can be formed by a class that extends Thread class
Answer: b) run() method is used to begin execution of a thread before start() method in special cases Explanation: Therun()
method defines the task for the thread, but thestart()
method must be called to begin execution.run()
is never used for starting execution directly. - Which of these keywords are used for the block to be examined for exceptions?
a) check b) throw c) catch d) try
Answer: d) try Explanation: Thetry
block is used to define the code that will be examined for exceptions.