Ubinetics Previous Years Solved Sample Placement Papers
-
What will be the output of the following Java code?
class newthread extends Thread { Thread t; newthread() { t1 = new Thread(this, "Thread_1"); t2 = new Thread(this, "Thread_2"); t1.start(); t2.start(); } public void run() { t2.setPriority(Thread.MAX_PRIORITY); System.out.print(t1.equals(t2)); } } class multithreaded_programing { public static void main(String args[]) { new newthread(); } }
A: truetrue
B: falsefalse
C: true
D: false
Ans: BExplanation:
This program was previously done by using Runnable interface, here we have used Thread class. This shows both methods are equivalent, we can use any of them to create a thread.
Output:
$ javac multithreaded_programing.java $ java multithreaded_programing falsefalse
-
Which one of the following is not an access modifier?
A: Protected
B: Void
C: Public
D: Private
Ans: BExplanation:
Public, private, protected and default are the access modifiers.
-
What will be the output of the following Java program?
final class A { int i; } class B extends A { int j; System.out.println(j + " " + i); } class inheritance { public static void main(String args[]) { B obj = new B(); obj.display(); } }
A: 2 2
B: 3 3
C: Runtime Error
D: Compilation Error
Ans: DExplanation:
Class A has been declared final hence it cannot be inherited by any other class. Hence class B does not have member i, giving compilation error.
Output:
$ javac inheritance.java Exception in thread "main" java.lang.Error: Unresolved compilation problem: i cannot be resolved or is not a field
-
What is the numerical range of a char data type in Java?
A: 0 to 256
B: -128 to 127
C: 0 to 65535
D: 0 to 32767
Ans: CExplanation:
Char occupies 16-bit in memory, so it supports 216 i:e from 0 to 65535.
- Which of the following methods are methods of the String class?
a) delete( )
b) append( )
c) reverse( )
d) replace( )
Answer: d) replace( )
Explanation: The
replace()
method is part of the String class, while the others are not. - Which of the following methods cause the String object referenced by
s
to be changed? a) s.concat( ) b) s.toUpperCase( ) c) s.replace( ) d) s.valueOf( ) Answer: a) and b) Explanation:concat()
andtoUpperCase()
return new strings; however, they modify the string indirectly since strings in Java are immutable. - Is String a wrapper class? a) True b) False Answer: b) False Explanation: String is not a wrapper class. Wrapper classes in Java are used to wrap primitive types, but String is used to represent sequences of characters.
- What is an Applet? Should applets have constructors? Answer: Applet is a dynamic and interactive program that runs inside a Web page displayed by a Java-capable browser. We don’t have the concept of Constructors in Applets.
- How can I arrange for different applets on a web page to communicate with each other? Answer: Name your applets inside the Applet tag and invoke AppletContext’s getApplet() method in your applet code to obtain references to the other applets on the page.
- How do I select a URL from my Applet and send the browser to that page?
Answer: Ask the applet for its applet context and invoke showDocument() on that context object.
Example: URL targetURL; String URLString; AppletContext context = getAppletContext(); try { targetURL = new URL(URLString); } catch (MalformedURLException e) { // Code for recovery from the exception } context.showDocument(targetURL);
- Can applets on different pages communicate with each other? Answer: No. Not directly. The applets will exchange the information at one meeting place, either on the local file system or at a remote system.
- How do Applets differ from Applications?
Answer:
- Appln: Stand Alone
- Applet: Needs no explicit installation on the local machine
- Appln: Execution starts with the
main()
method - Applet: Execution starts with the
init()
method - Appln: May or may not have a GUI
- Applet: Must run within a GUI (Using AWT)
- What are the Applet’s Life Cycle methods? Explain them?
Answer: init() method - Called when an applet is first loaded.
Applets have a defined life cycle including the
init()
,start()
,stop()
, anddestroy()
methods.