Object Oriented Programming with Java Question Bank_Sept 21.docx
Object Oriented Programming with Java Question Bank_Sept 21.docx
Contents
BASIC 1
method? 6
CLONE REFLECTION API 8
COLLECTION API 8
EXCEPTION 11
FILE HANDLING 19
GENERICS 23
INHERITANCE 24
JAVA FX 40
MULTITHREADING 41
OOPS 47
SOCKET PROGRAMMING 64
BASIC
1. What is the output of the following code?
inti = 16; intj = 17;
System.out.println("i >> 1 = " + (i >> 1)); System.out.println("j >> 1 = " + (j >> 1)); A.
Prints "i >> 1 = 8"
"j » 1 = 8"
B. Prints "i >> 1 = 7" "j » 1 = 7"
C. Prints "i >> 1 = 8"
>> 1 = 9"
D. Prints "i >> 1 = 7"
"j >> 1 = 8"
7.. Which of the following are the correct signatures for method main()?
A. public static void main()
B. public static int main(String arg[])
C. public static void main(String arg[])
D. public static void main(String args[])
E. private static void main(String args[])
10. Which line out of the following will compile without a warning or an error?
A. boolean b=null; B. float f=1.3; C. byte b=257; D. int i=10;
14. What will be printed out if you attempt to compile and run the following code int i=1; switch
(i)
{ case O:
System.out.prinUn("zero"); break; case 1:
System.out.println("one"); case 2:
System.out.prinUn("two"); default:
System.out.println("default");
}
A. One B. one, default C. one,two,default D. Default
15. If a local variables of a method shop() belonging to a class called Walmart has the same name as a data
member of Walmart, which value is used when shop() is executing?
A. the local variable’s B. the class variable’s
C. the data member’s D. None of the above since this would cause a compiler error
3
USM’s Shriram Mantri Vidyanidhi Info Tech Academy
Object Oriented Programming with Java eDAC Sept 21
AWT
1. Adapter class is not available for
4
USM’s Shriram Mantri Vidyanidhi Info Tech Academy
Object Oriented Programming with Java eDAC Sept 21
}
What will happen?
A. compiler error “add method must take 2nd argument as GridBagConstraints”
B. exception during runtime
C. Button will appear according to gridwidth and gridheight specified
D. Button will appear but not according to gridwidth and gridheight specified.
5. Given setLayout(new BorderLayout()); add("south",new TextField(20)); What will happen to the above
code?
A. compiler error
B. textfield will be displayed properly at south
C. exception
D. textfield will be displayed in the center,since u have given illegal argument.
5
USM’s Shriram Mantri Vidyanidhi Info Tech Academy
Object Oriented Programming with Java eDAC Sept 21
7. Given public class Trial extends Frame
{
public Trial(String mess)
{
MenuBar mb=new MenuBar();
// here
}
}
How will u add “mb” to the frame?
A. addMenuBar(mb); B. setMenuBar(mb); C. mb.addMenuBar(); D. add(mb);
9. What is sent to the user via HTTP, invoked using the HTTP protocol on the user’s computer and run on
the user’s computer as an application?
A. A Java application B. A Java applet
C. A Java Servlet D. None of the above
10. java.awt.Component class method getLocation() returns Point (containg x and y cordinate).What does
this x and y specify
A. Specify the position of components lower-left component in the coordinate space of the component's
parent.
B. Specify the position of components upper-left component in the coordinate space of the
component's parent.
C. Specify the position of components upper-left component in the coordinate space of the screen. D.
None of the above
11. When u invoke “repaint()”, for a lightweight component , the AWT package calls which component
method?
A. repaint() B. update() C. paint() D. draw()
12. Which of the following the valid way to embed an applet class named myapplet into a web page.
A. <applet class=myapplet.class width=100 height=100> </applet>
B. <applet code=myapplet width=100 height=100> </applet>
C. <applet code=myapplet.class height=100 width=100 > </applet> D. <applet param=myapplet.class
width=100 height=100> </applet>
15. What does the following line of code do? TextField tf=new TextField(30);
A. This code is illegal , as there is no such constructor available inside “TextField” class.
6
USM’s Shriram Mantri Vidyanidhi Info Tech Academy
Object Oriented Programming with Java eDAC Sept 21
B. Creates the TextField object, that can hold 30 rows, but since it is not initialized to
anything, it will be always empty.
C. Creates a new TextField object that is 30 columns of text.
D. This code creates a TextField object that can hold 30 rows of text
COLLECTION API
1. One of the following throws ConcurrentModificationException if we try to modify while iterating over it.
A: Hashtable B: CopyOnWriteArrayList
C: ArrayList D: ConcurrentHashMap
2. The default capacity and load factor for Map implementations are
A: 12 and 0.60 B: 16 and 0.75 C: 20 and 0.75 D: 18 and 0.60
3. Given
Class Animal{void eat(){}}
Class Dog extends Animal{}
8
USM’s Shriram Mantri Vidyanidhi Info Tech Academy
Object Oriented Programming with Java eDAC Sept 21
Class Cat extends Animal{}
Void disp(List<? super Dog> mylist)
Which of the following is the wrong argument to disp ?
A: ArrayList of Animal B: ArrayList of Dog
C: ArrayList of Object D: All the above are correct arguments.
5. Which collection class allows you to grow or shrink its size and provides indexed access to its elements,
but whose methods are not synchronized?
A: java.utiI.HashSet B: java.utiI.LinkedHashSet
C: java.utiI.List D: java.util.ArrayList
6. Which of the following class uses String as key to store the value in object?
a) Dictionary b) Array c) ArrayList d) Properties
8. ________ can be used to control the order of certain data structure and collection of object too.
a) Serial comparators b) natural comparators
c) comparators d) all of the above
C. A set may contain elements that return duplicate values from a call to the equals method
D. Duplicate values will cause an error at compile time
10. What is the sequence followed by HashMap or HashSet while adding or retrieving entries.
A: ==, equals(), hashcode() B: equals(), == , hashcode() C: hashcode() , == , equals() D: none of
these
11. If you try to invoke “remove()” method on iterator of CopyOnWriteArrayList , it raises following
exception
A: ConcurrentModificationException B: UnsupportedOperationException C: IllegalOperationException
D: none of these
9
USM’s Shriram Mantri Vidyanidhi Info Tech Academy
Object Oriented Programming with Java eDAC Sept 21
14. In get () or put() of map implementation equals () is Called before ==.
A. True B. False
18. When you add any object inside Collection API implementation class, its copy is added.
A. True B. False
20. In map implementation when hashcode of two keys are same it is called as?
A. Hashing B. Hash Collision C. Hash Clash D. None of these
21. One of the following allows us to define more than one strategies.
A. Comparator B. None of these C. Enumeration D. Comparable
24. Suppose that you would like to create an instance of a new Map that has an iteration
order that is the same as the iteration order of an existing instance of a Map. Which concrete
implementation of the Map interface should be used for the new instance?
A. TreeMap B. HashMap C. LinkedHashMap
25. Which class does not override the equals() and hashCode() methods, inheriting them directly from class
Object?
A. java.lang.String B. java.lang.Double
C. java.lang.StringBuffer D. java.lang.Character
26. What will happen if you compile/run the folowing lines of code? Vector a = new Vector();
a.addElement(10);
System.out.println(a.elementAt(0)_);
A. Prints 10. B. Prints 11.
C. Compilation error at line 3. D. Prints some garbage.
EXCEPTION
1. Given Following code: import java.io.*; class
sub extends base
{
void disp()throws IOException
{
}
}
class base
{
void disp()throws Exception
{
}
}
public class myclass
{
public static void main(String args[])
{ try
{
base b=new sub(); b.disp();
}
catch(Exception ee)
{
System.out.println(ee);
}
System.out.println(“done”);
}
}
11
USM’s Shriram Mantri Vidyanidhi Info Tech Academy
Object Oriented Programming with Java eDAC Sept 21
A. warning B. compilation error C. runtime error D. output
“done”
5. Imagine there are two exception classes Exception1 and Exception2 derived from the Exception class.
Given these two definitions:
class First
{
void test()throws Exception1,Exception1
{
}
}
class Second extends First
{
void test()
{
}
}
Now define a class “Third” derived from “Second” and override “test ()” method inside it. What
exceptions can Third’s test() method throw?
A. Exception1 B. Exception2
C. No checked exceptions D. it can declare any checked
6. What letters get written to the standard output with the following code? public class MyClass
{
public static void main(String args[])
{ try
{
method();
}
catch(Exception ie)
{
}
}
static void method()
12
USM’s Shriram Mantri Vidyanidhi Info Tech Academy
Object Oriented Programming with Java eDAC Sept 21
{ try
{
wrench();
System.out.println("a");
}
catch(ArithmeticException ae)
{
System.out.println("b");
}
finally
{
System.out.println("c");
}
System.out.println("d");
}
static void wrench()
{
throw new NullPointerException();
}
}
A. A B. b C. c D. Compilation error
13
USM’s Shriram Mantri Vidyanidhi Info Tech Academy
Object Oriented Programming with Java eDAC Sept 21
{ try
{
System.out.println("Hey There");
} finally
{
System.out.println("in Finally");
} }}
What will happen when one tries to compile and run above code?
A. Compilation Fails
B. The program will print Hey There, then will print in finally.
C. The program will print Hey There, then will print that an Exception has occurred, and then will print in
finally.
D. None of them
10 Given:
1. public class Foo {
2. public static void main(String[] args) {
3. try {
4. return;
5. } finally {
6. System.out.println( "Finally" );
7. } 8. } 9. }
What is the result?
A. Finally B. Blank C. Null D. None of the above
11. In exception handling mechanism, finally block is always executed, even if no exception occurred in the try
block
A. True B. False
}
}
A. compile error
B. neither compilation nor runtime error
C. no compilation error but exception at runtime.
15. Which statement is true,if the following program is run by java test10 ? public class test10
{
public static void main(String []args)
{
String []num={"one","two","three","four"}; if(args.length==0)
{
System.out.println("Zero");
}
else {
System.out.println(num[args.length]+" arguments”);
}
15
USM’s Shriram Mantri Vidyanidhi Info Tech Academy
Object Oriented Programming with Java eDAC Sept 21
}
}
A. The program won’t run because argument of main is not properly mentioned
B. The program will throw a NullPointerException
C. The program will display Zero when executed
D. The program will display 0 arguments when executed
16. following program will not print “==” public class test12
{
Public static void main(String args{})
{
String first=”abc”;
String second=new String(first);
If(first==second)
{
System.out.println(“==”);
}
}
}
A. True B. False
17. Assuming a method contains code which may raise an Exception (but not a RuntimeException), what is
the correct way for a method to indicate that it expects the caller to handle that exception:
A. throw Exception B. throws Exception
C. new Exception D. Don't need to specify anything
18. What is the result of executing the following code, using the parameters 4 and 0:
public void divide(int a, int b)
{ try {
int c = a / b;
}
catch (Exception e)
{
System.out.print("Exception ");
} finally
{
System.out.println("Finally");
}
A. Prints out: Exception Finally B. Prints out: Finally
C. Prints out: Exception D. No output
21Can you declare method local variable as final and can an abstract class may be final?
A. Yes, yes B. Yes, no C. No, yes D. No, no
22. Which of these methods of String class is used to obtain character at specified index?
A. char() B. charOn() C. charat() D. charAt()
23. What will happen in the below code snipet: public class MyClass
{
int i; float f; double d;
boolean bl;
public static void main(String args[])
{
System.out.println("int = "+i);
System.out.println("float = "+f);
System.out.println("double = "+d);
System.out.println("boolean = "+bl);
}
}
A. Int=0 float=0.0 double=0.0 boolean=false
B. Compilation error: cannot make static reference to the non-static field
C. Int=0 float=0.000 double=0.000 boolean=false
D. Compilation error: variable may not have been initialized
32. If u want to create checked exception as user defined exception u need to extend
A. RuntimeException B. Throwable C. Exception D. Error
33. When u write one try and multiple catch the most specific catch should precede the most generic catch
A. True B. False
34.
class exception_handling
{
public static void main(S1ring argsO)
{
try
{
System.out.print("Hello" + " " + 1IO) ;
} finally
{
System.out.print("World");
}
}
}
A. Hello B. World
C. Compilation Error D. First Exception then World
18
USM’s Shriram Mantri Vidyanidhi Info Tech Academy
Object Oriented Programming with Java eDAC Sept 21
FILE HANDLING
1. One of the following class provides “seek ()” method
A: FileInputStream B: File C: RandomAccessFile D: FileReader
2. Given
File f=new File("abc.txt");
FileInputStream fis=new FileInputStream(f); byte arr[]=new byte[100]; which statement
will read content of “abc.txt” into arr.
A: arr=fis.read() B: f.read(arr) C: arr=f.read() D: fis.read(arr)
5. Classes that do not implement ______interface will not have any of their State serialize or deserialized.
A: List B: SingIeThreadModeI C: Serializable D: Comparable
C. for(int i=0;i<=9;i++)
i. {
i. fos.writeInt(i);
ii. }
D. DataOutputStream dos=new DataOutputStream(fos);
a. for(int i=0;i<=9;i++)
{
dos.writeInt(i);
}
10. What is the permanent effect on the file system of writing data to a new FileWriter("report"), given the
file report already exists?
A. The data is appended to the file
B. The file is replaced with a new file
C. An exception is raised as the file already exists
20
USM’s Shriram Mantri Vidyanidhi Info Tech Academy
Object Oriented Programming with Java eDAC Sept 21
D. The data is written to random locations within the file
13. Given
class base
{
int k;
}
class sub implements Serializable
{
base b=new base();
int j;
}
If we try to serialize instance of sub class,
A: sub as well as base state will be serialized
B: NotSerializableException
C: only sub instance will be serialized
D: compiler error “ cannot serialized object having non-serializable parent”
21
USM’s Shriram Mantri Vidyanidhi Info Tech Academy
Object Oriented Programming with Java eDAC Sept 21
C. BufferedWriter D. none of the above
21. In case of Serializable when u deserialize an object constructor does not get invoked.
A. True B. false
25. in case of Externalizable when u deserialize an object first readExternal() is called and then constructor
is called.
A. True B. false
27. If inner class implements Externalizable we don’t get any problem while deserialization A. True
B. false
28. If static nested class implements Externalizable we don’t get any problem while deserialization
A. True B. false
31. Which of these classes are used by character streams for input and output operations?
A. lnputStream B. Writer C. ReadStream D. lnputOutputStream
GENERICS
1. At the time of compilation compiler removes all the information about generics. This is known as A.
Generic-removal B. Generic-Erasure C. Type-Erasure D.none of the above
6. If the base class reference referring to sub class array then there is a possibility of
A. IllegalArrayException B. ArrayStoreException
C. NullPointerException D. none of the above
10. List <? Super Dog> mylist=new ArrayList<Animal>() mylist.add(new Cat()); will work
A. Yes B. no
23
USM’s Shriram Mantri Vidyanidhi Info Tech Academy
Object Oriented Programming with Java eDAC Sept 21
INHERITANCE
1. What is the output of following code.
class a
{
static
{
System.out.println(" static a");
}
}
class b extends a
{
static
{
System.out.println(" static b");
}
}
class c extends b
{
static
{
System.out.println(" static c");
}
}
public class myclass
{
static
{
System.out.println(" static myclass");
}
public static void main(String args[])
{
new c();
System.out.println("in main");
}
}
27
USM’s Shriram Mantri Vidyanidhi Info Tech Academy
Object Oriented Programming with Java eDAC Sept 21
7. Why multiple inheritance is not available in java?
A. It leads to confusion for a Java program
B. The programmer can achieve multiple inheritance by using interface
C. The programmer can achieve multiple inheritance by repeatedly using single inheritance D.All of
the above
}
}
A. base B. sub3 C. sub1 D. sub2
9. Given the following code,what can be said about the statement s=(sub)b ? class base
{
}
class sub extends base
28
USM’s Shriram Mantri Vidyanidhi Info Tech Academy
Object Oriented Programming with Java eDAC Sept 21
{
}
public class test12
{
public static void main(String args[])
{
base b=new base(); sub s=new sub();
s=(sub)b;
}
}
A. legal at compile time but illegal at runtime
B. illegal at compile time
C. legal at compile and runtime ,but (sub) cast is not needed D. legal at compile and runtime ,but (sub)
cast is strictly needed.
10. What will happen when you attempt to compile or run this code? class Base
{
public final void amethod ()
{
system.out.println (“amethod”);
}
}
public class Fin extends Base
{
public static void main (String argv [] )
{
Base b = new Base() ; b.amethod
() ;
}
}
A. Compile time error indicating that a class with any final methods must be declared final itself
B. Compile time error indicating that you inherit from a class with final methods.
C. Run time error indicating that Base is not defined as final.
D. Success in compilation and output of “amethod” at run time
14. How restrictive is the default accessibility compared to public, protected and private accessibility? a.
Less restrictive than public.
A. More restrictive than public, but less restrictive than protected
B. More restrictive than private
C. More restrictive than protected, but less restrictive than private
D. Less restrictive than protected from within a package, and more restrictive than protected from outside
a package
15. What will be the output of the following code? public class VerySmart
{
public static void main(String[] args)
30
USM’s Shriram Mantri Vidyanidhi Info Tech Academy
Object Oriented Programming with Java eDAC Sept 21
{
String message;
System.out.println("message length is : " + message.length() );
}
}
A. /0 B. 0 C. compile time error D. run time error
16. The programmer must explicitly create the System.in and System.out objects.
A. True B. False
17. A method within a class is only accessible by classes that are defined within the same package as the
class of the method. How can such a restriction be enforced?
A. Declare the method with the keyword “public”
B. Declare the method with the keyword “protected”
C. Do not declare the method with any modifiers.
D. Declare the method with the keyword “private”
E. Declare the method with the keyword “package”
31
USM’s Shriram Mantri Vidyanidhi Info Tech Academy
Object Oriented Programming with Java eDAC Sept 21
{
i+=v*2;
}
}
public class test11
{
public static void main(String args[])
{
base b; b=new sub(); b.print();
}
}
A. 4 B. 3 C. Error: super has to be on first line of constructor D. 2
}
6. Test t=new Test();
7. System.out.println(t.str);
8. t.str=null;
9. t=null;
10. System.out.println("done");
At which line the object created at 1 will be marked for garbage collection?
A: Line 3 B: Line 4 C: Can’t say exactly when D: both Line3 and Line4
32
USM’s Shriram Mantri Vidyanidhi Info Tech Academy
Object Oriented Programming with Java eDAC Sept 21
int num=10;
void change(Trial ref)
{ ref.num=20;
ref=new Trial();
ref.num=30; ref=null;
}
public static void main(String args[])
{
Trial t=new Trial(); t.change(t);
System.out.println(t.num);
}
}
A: 30 B: 20 C: NullPointerException D: 10
if(str1.equals(str3))
{
System.out.println("str1 and str3 are equals");
}
else
{
System.out.println("str1 and str3 are not equals");
}
A: str1 and str3 are ==, str1 and str3 are equals
B: str1 and str3 are not ==, str1 and str3 are equals
C: str1 and str3 are ==, str1 and str3 are not equals
D: compilation error
30. Super need not be on first line if we want to invoke base class method.
A. True B. False
35
USM’s Shriram Mantri Vidyanidhi Info Tech Academy
Object Oriented Programming with Java eDAC Sept 21
47. class
A
{
int i;
void display()
{
System.out.println(i);
}
}
class B extends A { int j;
void display() { System.out.println(j));
}
}
class inheritance_demo {
public static void main(Stri111g argsO)
{
B obj = new B();
obj.i=1; obj.j=2;
obj.display();
}
}
Output of this program is
A.O B. 1 C. 2 D. Compilation Error
49. How can we ensure that a class will not be inherited from?
A. Delare it as constant B. Declare it as final
C. Declare it as static D. None of the above
52. Which modifier would be used to limit the methods visibility to only the ot the current oackaae and all
subclasses.
36
USM’s Shriram Mantri Vidyanidhi Info Tech Academy
Object Oriented Programming with Java eDAC Sept 21
54. To compare whether two references point to the same object we use
A. == operator B. equals function C. we can use both D. none of the above.
57. A class can be declared as________ if you do not want the class to be subclassed. Using the
_________keyword we can abstract a class interface from its implementation
A. protected, interface B. final, interface C. public, friend D. final, protected
37
USM’s Shriram Mantri Vidyanidhi Info Tech Academy
Object Oriented Programming with Java eDAC Sept 21
59. What is the output of the below program?
public class A
{
public void foo()
{
System.out.println(“foo”);
}
public void foo(int a)
{
System.out.println(“foo(int)”);
}
}
public class B extends A
{
public void foo()
{
foo(5);
}
public void foo(int a)
{
System.out.println(“fooB(int)”);
}
}
public class test
{
Public static void main(String[] args)
{
A a = new B() ; a.foo()
;
}
}
A. Program will not compile B. fooB(int) C. foo(int) D. foo
A. 5
B. 7
6
8
C. 6
D. Program will not compile
JAVA FX
1. In JavaFX following class is acting as a container for all the contents
A. Scene B. Stage C. LayoutPane D.None of the above
2. In order to start every JavaFX application you must invoke following method
A. Init() B. Start() C. Launch() D. None of the above
40
USM’s Shriram Mantri Vidyanidhi Info Tech Academy
Object Oriented Programming with Java eDAC Sept 21
MULTITHREADING
1. One of the following method is not executed by the programmer while writing multithreaded
applications.
A: start B: sleep C: join D: run
4. The___________ interface should be implemented by any class whose instances are intended to be
executed by a thread.
A: Serializable B: Comparable C: Collection D: Runnable
7. Given
public class TestOne {
public static void main (String[] args) {
Thread.sIeep(3000);
System.out.printIn("sleep");
}
}
A: No error, prints sleep B: Compilation error
C: Runtime Error D: No error & no output
10. Which type of instanceof does targetObject have to pass for this to be legal while using
Thread t=new Thread(targetObject);
A. targetObject instanceof Thread B. targetObject instanceof Applet
C. targetObject instanceof Object D. targetObject instanceof Runnable
11. __________ are utilized to control the access to an object especially in multithreaded programming?
A. Asynchronized methods B. serialized methods
C. synchronized methods D. both a and c
12. ___________ means each method in multithreaded environment doesn’t access data by multiple
threads at the same time.
A. Thread detach B. thread isolation C. thread safety D. thread lock
13. Which of the following starts the default thread available in java program?
A. System class B. main method C. static keyword D. none of these
14. Which two can be used to create a new Thread?
A. Extend java.lang.Thread and override the run method.
B. Extend java.lang.Runnable and override the start method.
C. Implement java.lang.thread and implement the run method.
D. Implement java.lang.Runnable and implement the run method.
42
USM’s Shriram Mantri Vidyanidhi Info Tech Academy
Object Oriented Programming with Java eDAC Sept 21
C. Ensures that two or more processes will start and end at the same time D. Ensures that
two or more Threads will start and end at the same time
16. What will happen when you attempt to compile and run the following code?
public class Bground extends Thread
{
public static void main(String argv[])
{
Bground b = new Bground(); b.run();
}
public void start()
{
for (int i = 0; i<10; i++)
{
System.out.println("Value of i = " + i);
}
}
}
A. A compile time error indicating that no run method is defined for the Thread class
B. A run time error indicating that no run method is defined for the Thread class
C. Clean compile and at run time the values 0 to 9 are printed out
D. Clean compile but no output at runtime
19. In case of class lock, non-static synchronized methods come into picture.
A. False B. true
22. One of the following method has to be invoked by the programmer in order to bring thread from born
to runnable state.
A: start B: sleep C: join D: run
44
USM’s Shriram Mantri Vidyanidhi Info Tech Academy
Object Oriented Programming with Java eDAC Sept 21
29. One of the following methods programmer never invokes in case of multi-threading application
A. Run B. start C. wait D. notify
30. We can invoke wait, notify or notify all from non-synchronized methods
A. True B. false
33. All the blocking methods i.e. sleep, wait and join can throw
A. IllegalMonitorStateException B. InterruptedException
C. BlockingException D. none of the above
45
USM’s Shriram Mantri Vidyanidhi Info Tech Academy
Object Oriented Programming with Java eDAC Sept 21
}
}
A. Output “MyTarget run” B. No output
C. Compilation error D. IllegalMonitorException during runtime
37. A class which contains non-static synchronized methods or blocks is called as_________
A. Singleton B. Synchronized C. Thread-Safe D. none of the above
38. _______ method makes caller thread wait till this thread die.
A. Wait B. sleep C. yield D. join
46
USM’s Shriram Mantri Vidyanidhi Info Tech Academy
Object Oriented Programming with Java eDAC Sept 21
39. The job that is done by the thread is decided by
A. run method B.start method C.main method D.None of the above
39.class multithreaded_progr.aming
{
public static void main(String argsO)
{
Thread t = Thread.currentThread(); System.out.println(t);
}
}
This will call the toString method of
A. Thread class B. Object class C. String class D. none of the above
OOPS
1. What is the output?
public class Trial
{
int num=10;
void change(Trial ref)
{
ref.num=20;
ref=null;
}
47
USM’s Shriram Mantri Vidyanidhi Info Tech Academy
Object Oriented Programming with Java eDAC Sept 21
public static void main(String args[])
{
Trial t=new Trial(); t.change(t);
System.out.println(t.num);
}
}
A: 20 B: 10 C: NullPointerException D: None of the above
11. Which of the following methods cause the string object referenced by s to be changed?
A: s.concat() B: s.touppercase() C: s.repIace() D: None of the above
12. Given
{
public static void rnain(String [] args)
{
PassA p = new PassA(); p.start();
}
void start()
{
long [] a1 = {3,4,5}; long [] a2 = fix(a1);
System.out.print(a1 [0] + a1 [1] + a1 [2] + " ");
System.out.println(a2[0] + a2[1] + a2[2]);
}
long [] fix(long [] a3)
{
49
USM’s Shriram Mantri Vidyanidhi Info Tech Academy
Object Oriented Programming with Java eDAC Sept 21
a3[1] = 7'; return a3;
}
}
A: 1 2 1 5 B: 1 5 1 5 C: 3 4 5 3 7 5 D: 3 7 5 3 7 5
13. What is the result of the following code? import java.util.*; enum
Animals
{
DOG("woof"), CAT("meow"), FISH("burbIe");
String sound;
Animals(String s) { sound = s; }
}
public class test11 { static Animals a; public static void main(String [] args) {
System.out.println(a.DOG.sound + " " + a.FISH.sound);
}
}
A: Multiple compilation errors
B: woof burble
C: Compilation fails due to an error on line 3
50
USM’s Shriram Mantri Vidyanidhi Info Tech Academy
Object Oriented Programming with Java eDAC Sept 21
17. Which of the following is not abstract?
A. Thread B. Collection C. AbstractList D. List
18. To provide access to members of the class to another class in different package which access specifier
is used?
A. Public B. protected C. private D. no modifier
21. Which of the following statement is supported by an Anonymous inner class supports?
A. It can extend exactly one class and implement exactly one interface
B. It can extend exactly one class and can implement multiple interfaces
C. It can extend exactly one class or implement exactly one interface
D. It can implement multiple interfaces regardless of whether it also extends a class.
22. Which string instance method would return true when invoked liked this: a.method(b) where
a=”BUTTERfly” and b=”butterFLY”
a) equalsIgnoreCase() b) toUpperCase() c) toLowerCase() d) equals()
System.out.println(sb1==sb2);
System.out.println(sb1.equals(sb2));
System.out.println(sb1.equals(ss1));
51
USM’s Shriram Mantri Vidyanidhi Info Tech Academy
Object Oriented Programming with Java eDAC Sept 21
System.out.println("Poddar".substring(3));
}
}
26. Given following code, what will happen to the output? public class MyClass
{
public static void main(String args[])
{
String str1="hello";
String str2="hel";
String str3=str2+"lo";
if(str1==str3)
{
}
if(str1.equals(str3))
{
not equals");
}
}
}
52
USM’s Shriram Mantri Vidyanidhi Info Tech Academy
Object Oriented Programming with Java eDAC Sept 21
c) str1 and str3 are ==
str1 and str3 are not equals
d) compilation error
28. Constructor is the class that does not provide information about, and access to, a single constructor of
a class.
A. True B. False
30. String s1=”hello”; String s2=”hello”; which one will return true
A. s1==s2 B. s1.equals(s2) C. both a and b
32. What is the correct ordering for the import, class and package declaration when found in a single file?
A. package, import, class B. class, import, package
C. import, package, class D. package, class, import
43. According to the new version of java, along with byte,short,int ,char following type is also allowed
A. Double B. float C. String D. none of the above
44. By-default value for the Reference type is:
A. false B. 0 C. null D.none of these
46. Just before object gets garbage collected following method is called
A. finalize() B. gc() C. main() D. none of the above
48. What will happen if static modifier is removed from the signature of the main method?
A. Compilation Error.
B. RunTime Error: NoSuchMethodError.
C. Program will compile and run without any output.
D. Program will compile and run to show the required output.
49. Under what conditions is an object's finalize() method invoked by the garbage collector? A.
When it detects that the object has become unreachable.
B. As soon as object is set as null.
C. At fixed intervalm it checks for null value.
D. None of the above.
51. Under what conditions is an object's finalize() method invoked by the garbage collector?
A. Just before object gets garbage collected. B. As soon as object is set as null.
C. At fixed intervalm it checks for null value. D. None of the above.
A. 1 B. 2 C. 3 D. 0
53. Which of the following is the correct syntax for suggesting the JVM performs garbage collection.
A. System.free (); B. System.setGarbageCollection () ;
C. System.out.get () ; D. System.gc ();
56 What will be the result of attempting to compile and run the following code? public class test3
{
static int a; int b;
public test3()
{
int c; c=a; a++; b+=c;
}
public static void main(String args[])
{
new test3();
}
}
55
USM’s Shriram Mantri Vidyanidhi Info Tech Academy
Object Oriented Programming with Java eDAC Sept 21
D. The code will fail to compile since the constructor is trying to use static field “c “ before
it has been initialized.
E. The code will compile and run without any problems.
57. What will happen if you compile/ run the following code? public
class Q 11
{
static String str1»= "main method with String[] args";
static String str2 = "main method with int[] args";
public static void main(String[] args) {
System.out.println(str1); .
}
public static void main(int[] args)
{
System.out.println(str2);
} }
A. Duplicate method main(), compilation error at line 6.
B. Duplicate method main(), compilation error at line 11.
C. Prints "main method with main String[] args".
D. Prints "main method with main int[] args".
61. What will happen if you compilel run the following code?
public class Q21
{
int maxElements;
void Q21()
{
maxElements = 100; ’
System.out. println(maxElements);
}
Q21 (int i)
{.
57
USM’s Shriram Mantri Vidyanidhi Info Tech Academy
Object Oriented Programming with Java eDAC Sept 21
maxElements = i;
System.out.println(maxElements);
}
public static void main(String[] args)
{ Q21 a = new Q21();
Q21 b = new Q21(999);
}
}
A. Prints 100 and 999.
B. Prints 999 and 100.
C. Compilation error at line 3, variable maxElements was not initialized.
D. Compilation error while calling parameterized constructor
62. What will happen if you invoke the following method? public
void check()
{
System.out.println(Math.min(-0.0,+0.0));
System.out.println(Math.max(-0.0,+0.0));
System.out.println(Math.min(-0.0,+0.0) == Math.max(0.0,+0.0));
}
A. prints -0.0, +0.0 and false. B. prints -0.0, +0.0 and true.
C. prints 0.0, 0.0 and false D. prints 0.0, 0.0 and true
63. What will be the output of the followingicode?
1. String s1 = "Java2";
2. String s2 = "Java2";
3. if (s1 == s2)
4. System.out.println("We are twins");
5. else
6. System.out.println("We are not twins“);
A. We are twins
B. We are not twins
C. The program will not compile.
D. The program will compile, but will produce a run-time error.
64. In the following code, which is the earliest statement, where the object originally held in e, may be
garbage collected: public class Test { public static void main (String'a"Fg§[]){ Employee e =_new
Employee("Bob", 48); e.calculatePay();
System.out.println(e.printDetails()); _ e
= null;
e = new Employee("Denise", 36);
e.calculatePay();
System.out.println(e.printDetails());
}
}
58
USM’s Shriram Mantri Vidyanidhi Info Tech Academy
Object Oriented Programming with Java eDAC Sept 21
71. class
A
{
int I;
public void m1()
{
System.out.println("value of i is" + i);
} }
public class Code1
{
public static void main(StringO args) {
A obj =new A(); A obj1 =new A(}; obj.I =3;
obj1. i=4;
System.out.prlntln{obj1.i +·" + obj.I);
}
this program will print
59
USM’s Shriram Mantri Vidyanidhi Info Tech Academy
Object Oriented Programming with Java eDAC Sept 21
72.
class A
{
public void m1()
{
System.out.println("1");
}
public static void m2()
{
m1(); System.out.prinUn("2");
}
}
when we call m2 function,here output will be
A. 1, 2 B. 2, 1 C. runtime error D. compile time error
60
USM’s Shriram Mantri Vidyanidhi Info Tech Academy
Object Oriented Programming with Java eDAC Sept 21
}
A. false true B. false false C. true false D. true true
61
USM’s Shriram Mantri Vidyanidhi Info Tech Academy
Object Oriented Programming with Java eDAC Sept 21
{ int c; public: void
setc(int n) {c=n;}
};
referring to the sample code above, how can you access the int member ”a” in class derived? A.
using member functions of base only.
B. only by using friend functions.
C. using member functions of derived only.
D. by using member functions of derived and base
E. by using any function.
81. If an instance of class A is created in what in what order will the numbered lines be hit public
class A
{
1: public int a = 1;
Public A()
{
2: a = 2;
}
{
3: a =3;
62
USM’s Shriram Mantri Vidyanidhi Info Tech Academy
Object Oriented Programming with Java eDAC Sept 21
}
}
A. Class will not compile B. 1 3 2 C. 2 1 3 D. 2 1
84. What is the name of the concept by which I can assign an int directly to an Integer ?
A. Casting
B. Auto Assignment C.
Auto boxing
D. It is not possible. Primitive type cannot be assigned to objects
SOCKET PROGRAMMING
1. Which of the following class allows Tcp Server to wait for client on a particular port?
A: InetAddress B: ServerSocket C: Socket D: none of the above
2. One of the following port range is valid for Network programming in java
A: 1 to 65535 B: 1023 to 65535 C: 1024 to 65535 D: 0 to 1023
3. Which one is used to send packet over the network in case of UDP?
A: DatagramPacket B: Socket C: DatagramServer D: DatagramSocket
6. Which of the following class allows UDP Server to wait for client on a particular port?
A: InetAddress B: DatagramSocket
C: DatagramPacket D: none of the above
8. Which method is used to wait for client to get connected in TCP? A: accept
B: receive
C: wait
D: socketWait
10. The class which is used to send the packet in case of UDP is
A. Socket B. UDPSocket C. UserDatagramPacket D. UserDatagramSocket
16. _______ class is used to make server wait for client request in TCP.
A. Socket
B. ServerSocket
C. SocketInputStream
D. none of these
18. Marshalling is
A. Converting packets into data
B. converting data into packets
C. converting bytes into character
20. What will be printed out if you attempt to compile and run the following code?
int i=9; switch
(i) { deiault:
System.out.println("default"); cazse
0:
Sy:stem.out.println("zero");
break; case 1:
65
USM’s Shriram Mantri Vidyanidhi Info Tech Academy
Object Oriented Programming with Java eDAC Sept 21
System.out.println("one"); case
2: system.out.println("two"); }
a) default
b) default, zero
c) error default clause not defined
d) no output displayed
21. Which of the following lines will compile without warning or error.
a) float f=1.3;
b) char c="a";
c) byte b=257; ‘
d) boolean b=null;
e) int i=10;
22. How to terminate JVM when I close all the application windows?
a. Systemexit(u) b. System.exit( 1) c. 3ystem.exit(2) d. All are invalid answers
66