Upload
rishiimca
View
218
Download
0
Embed Size (px)
Citation preview
8/9/2019 Java Mock4
1/25
1. What makes a Thread to stop execution
a. An interrupted exception is thrownb. Invoke sleep() methodc. Higher priority thread takes over
d. While a thread does a read() using inputstream
2. Which is the correct expression to be used to read line by line and store theresult into a String object
a) File f = new File("test.txt");
b. FileInputStream f = new FileInputStream("test.txt");c. DataInputStream d = new DataInputStream(newFileInputStream("test.txt"));
d. BufferedReader br = new BufferedReader(newInputStreamReader(new FileInputStream("test.txt")));
e. BufferedReader br = new BufferedReader(new InputStreamReader(newFileInputStream("test.txt", "8859_1)));
3. Which of the following will not throw a NullPointerException?String s = null;
a. if ((s!=null) & (s.length() > 0))b. if ((s!=null) && (s.length() > 0))c. if ((s==null) | (s.length() == 0))d. if ((s==null) || (s.length() == 0))
4. Which of the following describes a fully encapsulated class?
a. Methods cannot be privateb. Variables cannot be private
8/9/2019 Java Mock4
2/25
c. The state of the object can only be modified through accessormethod.
d. All variables are private.e. All variables and all methods are private
5. Which state decide a var. a which is suitable for referring to an array of 50string objects?
a. char a[ ] [ ];b. String a [ ];c. String [ ] a;
d. Object a [50];e. String a[50];
6. In the following program, ifsomecondition() is true, then only line number 3must throw a custom exception MyException. MyException is not a subclass ofruntime exceptions. What are the changes to be made to the given method.
1. Public void method() {2. If (somecondition()) {3. }4. }
a. Add throws new MyException(); in line number 3b. Add throws new MyException(); in line number 5c. Add throw new MyException(); in line number 3d. Modify the method declaration such that an object of type Exception
is to be thrown
8/9/2019 Java Mock4
3/25
7. What is the output of the following program
public class Example{
private int i;
public static void main(String args[]) {
method() ;
}
public static void method() {
int j;
j= i;
System.out.println("The value of i is " +i);
}
}
a. The program prints The value of i is 0;b. The program gives a compilation error. By changing the private int i topublic int i the problem gets resolved
c. The program gives a compilation error. By changing the private int ito private static int i the problem gets resolved
d. The program gives a compilation error. By changing the public staticof the method to public only and then calling this method from themain using an instance of Example, the problem gets resolved.
8. Which of the following are true?
Long L9 = new Long(9);
Long L10 = new Long(9);
8/9/2019 Java Mock4
4/25
Long L11 = L10;
long A = 9;
long B = 9;
a. L9 == L10b. L9 == L11c. L10 == L11d. A == L9e. A == B
9. How should we make a class to handle the events generated by a single userinterface component?
a. Subclassing a adapter class is inappropriate in this case - Falseb. Implements class which handles all the user interface listener
methods - True
10. What are the assignments which are valid in the line XXXX class Super{
private int i;public int ma_cd(float f){i = (int) f;return i; }}class Sub extends Super{int j;float g;public Sub(){Super s = new Super();
XXXX }}
a. j = i;b. g = f;c. j = s.ma_cd(3.2f);d. j = s.i;
8/9/2019 Java Mock4
5/25
e. g = s.f;
11. What is the output of the above code?
outer : for(int x = 0 ; x
8/9/2019 Java Mock4
6/25
3. String s2 = s+ s1;4. s = null;5. s = s1;6. s1 = s;
a. Before Line 3.b. Before Line 4.c. Before Line 5.d. Before line 6.
13. There is a plan to prepare a class which is going to used in many unrelatedparts of the project. The class should be Polygon object which is a Shape. Thepolygon has a information about the coordinates stored in a vector, color statuswhich states whether true or false. Write a class declaration which describes theabove
public class Polygon extends Shape{
vector coord;
boolean colorstatus;
}
14. There is an Employee who is a Person. The employee stores details in aVector, date employed and number of instances. What are the type of
variables that is to be added to the Employee class
a. Vectorb. Datec. Object
d. Persone. Employee
8/9/2019 Java Mock4
7/25
f. Int
15. What is the constructor argument for the FilterInputStream
a. Fileb. InputStreamc. DataInputStreamd. BufferedReadere. InputStreamReader
16. What is the hexadecimal representation of 7 (not more that 4 characters)
0x7
17. What is the output of the following program Example if args[0] is printed
java Example cat dog
a. dogb. catc. Exampled. javae. Null PointerException is thrown
18. What is the modifier for all the listener methods
a. privateb. default (no access modifier specified)c. protectedd. public
8/9/2019 Java Mock4
8/25
e. static
19. Which of the following is true regarding GridBagLayout
a. if a component has been given fill both is true, then as the containerresizes the component resizes
b. The number of rows and columns are fixed while loading the components.
c. The number of rows and columns are fixed while loading the layout itself .d. if a component has a non-zero weighty value, then the component grows in
height as the container is resized.
20. What is the value of x, if "Test 3" to be printed Switch(x){
Case 1: System.out.println("Test 1");
Case 2:
Case 3: System.out.println("Test 3"); break;
default : System.out.println("Test 4"); break;
}
a. 1b. 2c. 3d. 4
e. 0
8/9/2019 Java Mock4
9/25
21. If the string array name is argc in the arguments in the main method, which isinvoked by the interpreter when a program is executed
a. char string [ ] [ ]b. char [ ] a [ ]
c. char a[ ]d. String argce. String argv[ ]f. String argc[ ]
22. What is the argument of the KeyListener methods
KeyEvent
23. What is the correct declaration of an abstract method
a. public abstract method(); // no ret typeb. public void abstract method(): // position ret typec. public abstract void method() {} // no bodyd. public final abstract void method(); // final abs cannot come togethere. public abstract void method () ;
24. How will you override or overload the following method method1 of the Superclass in the class Sub?
class Super{
protected void method1() {}
}
class Sub extends Super{}
a. public void method1() { return 0; }b. public int method1() { return 0; }c. public void method1(StringBuffer s) { }d. public void method1() { }
8/9/2019 Java Mock4
10/25
e. public void method1(String s) { }
25. What is the value of x if Test 3 is to be printedif (x > 4) {System.out.println( "Test 1" );
} else if (x> 9){
System.out.println("Test 2");
} else
System.out.println("Test 3");
a. 0 to 4b. Less than 0c. 5 to 9d. Greater than 10
26. What is the range of a char type?0 216-1
27. What does >> and >>> denotes
A. >> is right shift >>> is roundB. >> is signed right shift >>> roundC. >> is signed shift and >>> is unsigned shiftD. >> is unsigned >>> is unsigned
28. What are valid keywords
8/9/2019 Java Mock4
11/25
a. NULLb. TRUEc. implementsd. interfacee. instanceof
f. sizeof
29. Which of the following should be used to have No Order, Duplication orPerfect retrieval mechanism
a. Map // no order, no dupe, perfect retrive
b. List //order, dupe, no retc. Set //no order, no dupe, not perfect retrieve
d. Collection //no order, dupes, no ret
30. In which LayoutManager, a component to be added to have only the widthresized but not the height
a. FlowLayoutb. GridLayoutc. GridBagLayoutd. East or West of BorderLayoute. North or South of BorderLayout
31. What is the access modifier for a class member variable which is to be usedin the same package where the class is defined
a. protected
b. privatec. public
d. no access modifier
8/9/2019 Java Mock4
12/25
e. static
32. What is the body of a Thread
a. runb. begin
c. execute
d. starte. resume
33. What is the output of the following code? int i = 0;
do {
System.out.println("The value of i is " + i);
}while(--i > 0)
System.out.println("Finished");
a. The value of i is 1b. The value of i is 0c. Finishedd. Compilation Error ( no ; after while condn);e. Runtime Error
34. What is the output of the following programclass Example{
static int arr[] = new int[10];
8/9/2019 Java Mock4
13/25
public static void main(String args[]){
System.out.println(" The value 4th element is " + arr[4]);
} }
a. The value of 4th element is 4b. The value of 4th element is nullc. The value of 4th element is 0d. Null Pointer exception is raisede. Runtime error, because the class is not instantiated
35. Which of the following is the correct class declaration for Car.java. See to thatit is a case-sensitive system
a. public class Car{
int in;
public Car(int inn){
in = inn
} }
b. public class Car extends Truck, Vehicle{
int in;
public Car(int inn){
in = inn;
} }
8/9/2019 Java Mock4
14/25
c. public class Car{
int in;
public abstract void raceCar() {System.out.println("RaceCar");}
public Car(int inn){
in = inn;
} }
d. import java.io.*;
public class Car extends Object{
int in;
public Car(int inn){
in = inn;
} }
36. Which of the following are true about Threads?
a. Threads can only be created by extending the java.lang.Threadb. Threads of the same program end togetherc. A thread which is suspended, cannot be restartedd. Uncoordination of threads, will result in the data be corrupted
e. Java Interepter will exit only when all non-demon threads are not ended
37. What is true about Garbage Collection mechanism
8/9/2019 Java Mock4
15/25
a. Garbage Collection releases memory at predictable times and atpredictable rates
b. A correct program is one in which does not depend on the memoryrelease by the garbage collector
c. A programmer can indicate to the gc mechanism by making
reference to a variable as nulld. Garbage collection ensures that there may not be any leakage of memory.
38. A question on Equality operatorOk
39. What is the output of the following program?
class Super{
String name;
Super(String s){
name =s ;
} }class Sub extends Super{
String name;
Sub(String s){
name=s;
}
public static void main(String args[]){
Super sup = new Super("First");
Sub sub = new Sub("Second");
System.out.println(sub.name + " " + sup.name);
8/9/2019 Java Mock4
16/25
}
}
a. First Second
b. Second Firstc. Compilation errord. Runtime error stating same name found in Super as well as in the Sub
class
40. An IOException needs to be thrown in some method of a program. Select the
correct expression for the raising an exception
a. new Exception();b. throw Exception();c. throw new(new IOException());d. throw new Exception(); // its actually throw new IOException();e. throws new Exception();
41. A Question on Method Overriding
OK
42. What is the output of the following program if method1() does not throw anyexception?
try{
method1();
System.out.println("First");
}catch (Exception e){
System.out.prinln("Second");
8/9/2019 Java Mock4
17/25
}finally{
System.out.println("Finally");
} System.out.println("Last");
a. First followed by Finallyb. First followed by Finally followed by Lastc. First followed by Lastd. First
43. What are the modifier which cannot be used for a local automatic variable
a. default ( no access modifier )b. finalc. staticd. public
44. What does getID() method of the event return
a. time of the eventb. source of the eventc. nature of the cause of the eventd. place of the event
45. What cannot be added to a container
a. Appletb. Panelc. Componentd. Container
8/9/2019 Java Mock4
18/25
e. MenuItem // can be added only to a Menu
46. How to invoke the constructor from a constructor in the same class
class Super{
float x;
float y;
float z;
Super(float a, float b){
x = a;
y = b;
}
Super(float a, float b, float c){
XXXX
z = c;
}
}
a. super(a,b);b. Super(a,b);c. this(a,b);
d. This(a,b);
47. What is true about inner classes?
8/9/2019 Java Mock4
19/25
a. Inner classes have to be instantiated only in the enclosing classb. Inner classes can access all the final variables of the enclosing classc. Inner classes cannot be staticd. Inner classes cannot be anonymous classe. Inner classes can extend another class.
48. What are the correct declaration for an inner class
a. private class Cb. new simpleinterface() { //Anonymous so v can use newc. new complexinterface(x) {
d. new innerclasses() extends someotherclass {e. new innerclasses extends someotherclass { //Not as we have given a name and
so v cannot use new
49. What are true about Listener interfaces
a. A component can have only one listener attached to it b. If a component has multiple listeners attached, the order in which thelisteners are invoked are unknown
c. If a component has multiple listeners, then all the listeners have to befriends
50. If a class member variable is initialized and the value is not be changed at all,what is the modifier to be used during the declaration of the variable
a. constb. fixedc. publicd. static
8/9/2019 Java Mock4
20/25
e. final
51. What will happen if the following assignment is made, take the above intoaccount?
class Parent
class Derived1 extends Parent
class Derived2 extends Parent
Parent p;
Derived d1;
p = d1;
a. Legal at compile time and illegal at run time b. Legal at compile time and possibly legal at run time c. Legal at compile time and definitely legal at run time d. Illegal at compile time
52. Which modifiers are to be used to obtain the lock on the object
a. publicb. privatec. staticd. synchornized
e. lock
53. What can be possibly used at Point X
8/9/2019 Java Mock4
21/25
// Point X
public class Example
a. import java.awt.*;
b. package local.util;c. class NoExample{}d. protected class SimpleExamplee. public static final double PI = 3.14;
54. What is the range of int type
-231
to 231
-1
55. What is the output of the following program?class Super{
String firstname;
String secondname;
Super(){}
Super(String f, String s){
firstname = f;
secondname = s;
}
public String toString(){
return "It is Monopoly Mr. " + firstname;
}}
class Sub extends Super{
String firstname;
String secondname;
8/9/2019 Java Mock4
22/25
Sub(String f, String s){
firstname = f;
secondname = s;
}
public String toString(){
return "It is Monopoly Mr. " + secondname + " "+ firstname;
}}
class Test{
public static void main(String args[]){
Super first = new Super("Scott", "McNealy");
Super second = new Sub("Bill","Gates");
System.out.println( second + " \n " +first);
}}
a. It is Monopoly Mr. Bill Gates
It is Monopoly Mr. Scott
b. It is Monopoly Mr. Gates Bill
It is Monopoly Mr. McNealy
c. It is Monopoly Mr. Gates Bill
It is Monopoly Mr. Scott
8/9/2019 Java Mock4
23/25
d. It is Monopoly Mr. Gates
It is Monopoly Mr. Scott McNealy
56. How to instantiate the class Inner at point XXXX
class Outer{
class Inner{
}
public static void main(String args[]){
XXXX
}
}
a. new Inner();b. Inner i = Outer.new Inner();
c. Outer.Inner i = new Outer().new Inner();d. Inner i = Outer(new Inner());
57. What is the output of the following programpublic class Example{
Stack s1;
Stack s2;
public static void main(String args[]){
new Example();
8/9/2019 Java Mock4
24/25
}
public Example() {
s1 = new Stack();
s2 = new Stack();
method1(s1,s2);
System.out.println(s1 + " " + s2);
}
public void method1(Stack ms1, Stack ms2){
ms2.push(new Long(100));
ms1 = ms2;
}
}
a. Compilation error since ms2 cannot be assigned to ms1. b. s1 [ ] s2 [ ]c. s1 [ ] s2 [100]
d. s1 [100] s2 [100]
58. What are the legal identifers in Java?
a. %employeeb. $employee
c. employ-eed. employee1e. _employee
8/9/2019 Java Mock4
25/25
59. Which of the following are true about the listener mathods in AWT? (C)
a. In awt listener menthods generally takes an argument which is aninstance of some subclass of java.awt.AWTEvent
b. When multiple listeners are added to a single component the order of
invocation of the listeners is not specified.c. A single component can have multiple listeners added to it.