Advanced Java
Multiple Choice Questions and Answers
1. Which of these class is used to make a thread?a) String
b) System
c) Thread
d) Runnable
Answer : c
Explanation : Thread class is used to make threads in java, Thread encapsulates a thread of execution.
To create a new thread the program will either extend Thread or implement the Runnable interface.
2. Which of these interface is implemented by Thread class?
a) Runnable
b) Connections
c) Set
d) MapConnections
Answer : a
Explanation : None.
3. Which of these method of Thread class is used to find out the priority given to a thread?
a) get()
b) ThreadPriority()
c) getPriority()
d) getThreadPriority()
Answer : c
Explanation : None.
5. Which of these method of Thread class is used to Suspend a thread for a period of time?
a) sleep()
b) terminate()
c) suspend()
d) stop()
Answer : a
Explanation : None.
6. Which function of pre defined class Thread is used to check weather current thread being checked is still running?
a) isAlive()
b) Join()
c) isRunning()
d) Alive()
Answer : a
Explanation : isAlive() function is defined in class Thread, it is used for implementing multithreading and
to check whether the thread called upon is still running or not.
7. What is the output of this program?
1. class multithreaded_programing {
2. public static void main(String args[]) {
3. Thread t = Thread.currentThread();
4. t.setName("New Thread");
5. System.out.println(t);
6. }
7. }
a) Thread[5,main]
b) Thread[New Thread,5]
c) Thread[main,5,main]
d) Thread[New Thread,5,main]
Answer : d
Explanation : None.
Output:
$ javac multithreaded_programing.java
$ java multithreaded_programing
Thread[New Thread,5,main]
8. What is the priority of the thread in output of this program?
1. class multithreaded_programing {
2. public static void main(String args[]) {
3. Thread t = Thread.currentThread();
4. t.setName("New Thread");
5. System.out.println(t.getName());
6. }
7. }
a) main
b) Thread
c) New Threadd) Thread[New Thread,5,main]
Answer : c
Explanation : The getName() function is used to obtain the name of the thread, in this code the name given to thread is ‘New Thread’.
Output:
$ javac multithreaded_programing.java
$ java multithreaded_programing
New Thread
9. What is the name of the thread in output of this program?
1. class multithreaded_programing {
2. public static void main(String args[]) {
3. Thread t = Thread.currentThread();
4. System.out.println(t.getPriority());
5. }
6. }
a) 0
b) 1
c) 4
d) 5
Answer : d
Explanation : The default priority given to a thread is 5.
Output:
$ javac multithreaded_programing.java
$ java multithreaded_programing
5
10. What is the name of the thread in output of this program?
1. class multithreaded_programing {
2. public static void main(String args[]) {
3. Thread t = Thread.currentThread();
4. System.out.println(t.isAlive());
5. }
6. }
a) 0
b) 1
c) true
d) false
Answer : c
Explanation : Thread t is seeded to currently program, hence when you run the program the thread becomes active & code ‘t.isAlive’ returns true.
Output:
$ javac multithreaded_programing.java
$ java multithreaded_programing
true
11. What is multithreaded programming?
a) It’s a process in which two different processes run simultaneously.
b) It’s a process in which two or more parts of same process run simultaneously.
c) Its a process in which many different process are able to access same information.
d) Its a process in which a single process can access information from many sources.
Answer : b
Explanation : multithreaded programming a process in which two or more parts of same process run simultaneously.
12. Which of these are types of multitasking?
a) Process based
b) Thread based
c) Process and Thread based
d) None of the mentioned
Answer : c
Explanation : There are two types of multitasking: Process based multitasking and Thread based multitasking.
13. Which of these packages contain all the Java’s built in exceptions?
a) java.io
b) java.util
c) java.lang
d) java.net
Answer : c
Explanation : None.
14. Thread priority in Java is?
a) Integer
b) Float
c) double
d) long
Answer : a
Explanation : Java assigns to each thread a priority that determines hoe that thread should be treated with respect to others.
Thread priority are integers that specify relative priority of one thread to another.
15. What will happen if two thread of same priority are called to be processed simultaneously?
a) Any one will be executed first lexographically
b) Both of them will be executed simultaneously
c) None of them will be executed
d) It is dependent on the operating system.
Answer : d
Explanation : In cases where two or more thread with same priority are competing for CPU cycles,
different operating system handle this situation differently. Some execute them in time sliced manner some depending on the thread they call.
16. Which of these statements is incorrect?
a) By multithreading CPU’s idle time is minimized, and we can take maximum use of it.
b) By multitasking CPU’s idle time is minimized, and we can take maximum use of it.
c) Two thread in Java can have same priority
d) A thread can exist only in two states, running and blocked.
Answer : d
Explanation : Thread exist in several states, a thread can be running, suspended, blocked, terminated & ready to run.
17. What is the output of this program?
1. class multithreaded_programing {
2. public static void main(String args[]) {
3. Thread t = Thread.currentThread();
4. System.out.println(t);
5. }
6. }
a) Thread[5,main]
b) Thread[main,5]
c) Thread[main,0]
d) Thread[main,5,main]
Answer : d
Explanation : None.
Output:
$ javac multithreaded_programing.java
$ java multithreaded_programing
Thread[main,5,main]
18. What is the priority of the thread in output of this program?
1. class multithreaded_programing {
2. public static void main(String args[]) {
3. Thread t = Thread.currentThread();
4. System.out.println(t);
5. }
6. }
a) 4
b) 5
c) 0
d) 1
Answer : b
Explanation : The output of program is Thread[main,5,main], in this priority assigned to the thread is 5. Its the default value.
Since we have not named the thread they are named by the group to they belong i:e main method.
Output:
$ javac multithreaded_programing.java
$ java multithreaded_programing
Thread[main,5,main]
19. What is the name of the thread in output of this program?
1. class multithreaded_programing {
2. public static void main(String args[]) {
3. Thread t = Thread.currentThread();
4. System.out.println(t);
5. }
6. }
a) main
b) Thread
c) System
d) None of the mentioned
Answer : a
Explanation : The output of program is Thread[main,5,main], Since we have not explicitly named the thread they are named by the group
to they belong i:e main method. Hence they are named ‘main’.
Output:
$ javac multithreaded_programing.java
$ java multithreaded_programing
Thread[main,5,main]
20. What is the name of the thread in output of this program?
1. class multithreaded_programing {
2. public static void main(String args[]) {
3. Thread t = Thread.currentThread();
4. System.out.println(t.isAlive());
5. }
6. }
a) 0
b) 1
c) true
d) false
Answer : a
Explanation : Thread t is seeded to currently program, hence when you run the program the thread becomes active & code ‘t.isAlive’ returns true.
Output:
$ javac multithreaded_programing.java
$ java multithreaded_programing
true
21. Which of these packages contain all the collection classes?
a) java.lang
b) java.util
c) java.net
d) java.awt
Answer : b
Explanation : None.
22. Which of these classes is not part of Java’s collection framework?
a) Maps
b) Array
c) Stack
d) Queue
Answer : d
Explanation : Queue is not a part of collection framework.
23. Which of these interface is not a part of Java’s collection framework?
a) List
b) Set
c) SortedMap
d) SortedList
Answer : d
Explanation : SortedList is not a part of collection framework.
24. Which of these methods deletes all the elements from invoking collection?
a) clear()
b) reset()
c) delete()
d) refresh()
Answer : a
Explanation : clear() method removes all the elements from invoking collection.
25. What is Collection in Java?
a) A group of objects
b) A group of classes
c) A group of interfaces
d) None of the mentioned
Answer : a
Explanation : A collection is a group of objects, it is similar to String Template Library (STL) of C++ programming language.
26. What is the output of this program?
1. import java.util.*;
2. class Array {
3. public static void main(String args[]) {
4. int array[] = new int [5];
5. for (int i = 5; i > 0; i--)
6. array[5-i] = i;
7. Arrays.fill(array, 1, 4, 8);
8. for (int i = 0; i < 5 ; i++)
9. System.out.print(array[i]);
10. }
11. }
a) 12885
b) 12845
c) 58881
d) 54881
Answer : c
Explanation : array was containing 5,4,3,2,1 but when method Arrays.fill(array, 1, 4, 8) is called it fills the index location
starting with 1 to 4 by value 8 hence array becomes 5,8,8,8,1.
Output:
$ javac Array.java
$ java Array
58881
27. What is the output of this program?
1. import java.util.*;
2. class vector {
3. public static void main(String args[]) {
4. Vector obj = new Vector(4,2);
5. obj.addElement(new Integer(3));
6. obj.addElement(new Integer(2));
7. obj.addElement(new Integer(5));
8. obj.removeAll(obj);
9. System.out.println(obj.isEmpty());
10. }
11. }
a) 0
b) 1
c) true
d) false
Answer : c
Explanation : firstly elements 3, 2, 5 are entered in the vector obj, but when obj.removeAll(obj); is executed all the elements are deleted and
vector is empty, hence obj.isEmpty() returns true.
Output:
$ javac vector.java
$ java vector
true
28. What is the output of this program?
1. import java.util.*;
2. class stack {
3. public static void main(String args[]) {
4. Stack obj = new Stack();
5. obj.push(new Integer(3));
6. obj.push(new Integer(2));
7. obj.pop();
8. obj.push(new Integer(5));
9. System.out.println(obj);
10. }
11. }
a) [3, 5]
b) [3, 2]
c) [3, 2, 5]
d) [3, 5, 2]
Answer : a
Explanation : push() and pop() are standard functions of the class stack, push() inserts in the stack and pop removes from the stack.
3 & 2 are inserted using push() the pop() is used which removes 2 from the stack then again push is used to insert 5 hence stack contains elements 3 & 5.
Output:
$ javac stack.java
$ java stack
[3, 5]
29. What is the output of this program?
1. import java.util.*;
2. class hashtable {
3. public static void main(String args[]) {
4. Hashtable obj = new Hashtable();
5. obj.put("A", new Integer(3));
6. obj.put("B", new Integer(2));
7. obj.put("C", new Integer(8));
8. obj.remove(new String("A"));
9. System.out.print(obj);
10. }
11. }
a) {C=8, B=2}
b) [C=8, B=2]
c) {A=3, C=8, B=2}
d) [A=3, C=8, B=2]
Answer : b
Explanation : None.
Output:
$ javac hashtable.java
$ java hashtable
{C=8, B=2}
30. What is the output of this program?
1. import java.util.*;
2. class Bitset {
3. public static void main(String args[]) {
4. BitSet obj = new BitSet(5);
5. for (int i = 0; i < 5; ++i)
6. obj.set(i);
7. obj.clear(2);
8. System.out.print(obj);
9. }
10. }
a) {0, 1, 3, 4}
b) {0, 1, 2, 4}
c) {0, 1, 2, 3, 4}
d) {0, 0, 0, 3, 4}
Answer : a
Explanation : None.
Output:
$ javac Bitset.java
$ java Bitset
{0, 1, 3, 4}
31. Which of these keywords is used to define packages in Java?
a) pkg
b) Pkg
c) package
d) Package
Answer : c
Explanation : None.
32. Which of these is a mechanism for naming and visibility control of a class and its content?
a) Object
b) Packages
c) Interfaces
d) None of the Mentioned.
Answer : b
Explanation : Packages are both naming and visibility control mechanism. We can define a class inside a package which is not accessible by code outside the package.
33. Which of this access specifies can be used for a class so that its members can be accessed by a different class in the same package?
a) Public
b) Protected
c) No Modifier
d) All of the mentioned
Answer : d
Explanation : Either we can use public, protected or we can name the class without any specifier.
34. Which of these access specifiers can be used for a class so that it’s members can be accessed by a different class in the different package?
a) Public
b) Protected
c) Private
d) No Modifier
Answer : a
Explanation : None.
35. Which of the following is correct way of importing an entire package ‘pkg’?
a) import pkg.
b) Import pkg.
c) import pkg.*
d) Import pkg.*
Answer : c
Explanation : Operator * is used to import the entire package.
36. Which of the following is incorrect statement about packages?
a) Package defines a namespace in which classes are stored.
b) A package can contain other package within it.
c) Java uses file system directories to store packages.
d) A package can be renamed without renaming the directory in which the classes are stored.
Answer : d
Explanation : A package can be renamed only after renaming the directory in which the classes are stored.
37. Which of the following package stores all the standard java classes?
a) lang
b) java
c) util
d) java.packages
Answer : b
Explanation : None.
38. What is the output of this program?
1. package pkg;
2. class display {
3. int x;
4. void show() {
5. if (x > 1)
6. System.out.print(x + " ");
7. }
8. }
9. class packages {
10. public static void main(String args[]) {
11. display[] arr=new display[3];
12. for(int i=0;i<3;i++)
13. arr[i]=new display();
14. arr[0].x = 0;
15. arr[1].x = 1;
16. arr[2].x = 2;
17. for (int i = 0; i < 3; ++i)
18. arr[i].show();
19. }
20. }
Note : packages.class file is in directory pkg;
a) 0
b) 1
c) 2
d) 0 1 2
Answer : c
Explanation : None.
Output:
$ javac packages.java
$ java packages
2
39. What is the output of this program?
1. package pkg;
2. class output {
3. public static void main(String args[])
4. {
5. StringBuffer s1 = new StringBuffer("Hello");
6. s1.setCharAt(1, x);
7. System.out.println(s1);
8. }
9. }
a) xello
b) xxxxx
c) Hxllo
d) Hexlo
Answer : c
Explanation : None.
Output:
$ javac output.java
$ java output
Hxllo
40. What is the output of this program?
1. package pkg;
2. class output {
3. public static void main(String args[])
4. {
5. StringBuffer s1 = new StringBuffer("Hello World");
6. s1.insert(6 , "Good ");
7. System.out.println(s1);
8. }
9. }
Note : Output.class file is not in directory pkg.
a) HelloGoodWorld
b) HellGoodoWorld
c) Compilation error
d) Runtime error
Answer : d
Explanation : Since output.class file is not in the directory pkg in which class output is defined, program will not be able to run.
output:
$ javac output.java
$ java output
can not find file output.class
41. Which of these keywords is used to define interfaces in Java?
a) interface
b) Interface
c) intf
d) Intf
Answer : a
Explanation : None.
42. Which of these can be used to fully abstract a class from its implementation?
a) Objects
b) Packages
c) Interfaces
d) None of the Mentioned.
Answer : c
Explanation : None.
43. Which of these access specifiers can be used for an interface?
a) Public
b) Protected
c) private
d) All of the mentioned
Answer : a
Explanation : Access specifier of interface is either public or no specifier. When no access specifier is used then default access specifier is used due to which interface is available only to other members of the package in which it is declared, when declared public it can be used by any code.
44. Which of these keywords is used by a class to use an interface defined previously?
a) import
b) Import
c) implements
d) Implements
Answer : c
Explanation : interface is inherited by a class using implements.
45. Which of the following is correct way of implementing an interface salary by class manager?
a) class manager extends salary {}
b) class manager implements salary {}
c) class manager imports salary {}
d) None of the mentioned.
Answer : b
Explanation : None.
46. Which of the following is incorrect statement about packages?
a) Interfaces specifies what class must do but not how it does.
b) Interfaces are specified public if they are to be accessed by any code in the program.
c) All variables in interface are implicitly final and static.
d) All variables are static and methods are public if interface is defined pubic.
Answer : d
Explanation : All methods and variables are implicitly public if interface is declared public.
47. Which of the following package stores all the standard java classes?
a) lang
b) java
c) util
d) java.packages
Answer : b
Explanation: None.
48. What is the output of this program?
1. interface calculate {
2. void cal(int item);
3. }
4. class display implements calculate {
5. int x;
6. public void cal(int item) {
7. x = item * item;
8. }
9. }
10. class interfaces {
11. public static void main(String args[]) {
12. display arr = new display;
13. arr.x = 0;
14.
14. arr.cal(2);
15. System.out.print(arr.x);
16. }
17. }
a) 0
b) 2
c) 4
d) None of the mentioned
Answer : c
Explanation : None.
Output:
$ javac interfaces.java
$ java interfaces
4
49. What is the output of this program?
1. interface calculate {
2. void cal(int item);
3. }
4. class displayA implements calculate {
5. int x;
6. public void cal(int item) {
7. x = item * item;
8. }
9. }
10. class displayB implements calculate {
11. int x;
12. public void cal(int item) {
13. x = item / item;
14. }
15. }
16. class interfaces {
17. public static void main(String args[]) {
18. displayA arr1 = new displayA;
19. displayB arr2 = new displayB;
20. arr1.x = 0;
21. arr2.x = 0;
22. arr1.cal(2);
23. arr2.cal(2);
24. System.out.print(arr1.x + " " + arr2.x);
25. }
26. }
a) 0 0
b) 2 2
c) 4 1
d) 1 4
Answer : c
Explanation : class displayA implements the interface calculate by doubling the value of item, where as class displayB implements the interface by
dividing item by item, therefore variable x of class displayA stores 4 and variable x of class displayB stores 1.
Output:
$ javac interfaces.java
$ java interfaces
4 1
50. What is the output of this program?
1.interface calculate {
2. int VAR = 0;
3. void cal(int item);
4. }
5. class display implements calculate {
6. int x;
7. public void cal(int item) {
8. if (item<2)
9. x = VAR;
10. else
11. x = item * item;
12. }
13. }
14. class interfaces {
15.
16. public static void main(String args[]) {
17. display[] arr=new display[3];
18.
19. for(int i=0;i<3;i++)
20. arr[i]=new display();
21. arr[0].cal(0);
22. arr[1].cal(1);
23. arr[2].cal(2);
24. System.out.print(arr[0].x+" " + arr[1].x + " " + arr[2].x);
25. }
26. }
a) 0 1 2
b) 0 2 4
c) 0 0 4
d) 0 1 4
Answer : c
Explanation : None.
output:
$ javac interfaces.java
$ java interfaces
0 0 4
51. Which of these classes is not included in java.lang?
a) Byte
b) Integer
c) Array
d) Class
Answer : c
Explanation : Array class is a member of java.util.
52. Which of these is a process of converting a simple data type into a class?
a) type wrapping
b) type conversion
c) type casting
d) None of the Mentioned.
Answer : a
Explanation : None.
53. Which of these is a super class of wrappers Double & Integer?
a) Long
b) Digits
c) Float
d) Number
Answer : d
Explanation : Number is an abstract class containing subclasses Double, Float, Byte, Short, Integer and Long.
54. Which of these is wrapper for simple data type float?
a) float
b) double
c) Float
d) Double
Answer : c
Explanation : None.
55. Which of the following is method of wrapper Float for converting the value of an object into byte?
a) bytevalue()
b) byte bytevalue()
c) Bytevalue()
d) Byte Bytevalue().
Answer : b
Explanation : None.
56. Which of these methods is used to check for infinitely large and small values?
a) isInfinite()
b) isNaN()
c) Isinfinite()
d) IsNaN()
Answer : a
Explanation : isinfinite() method returns true is the value being tested is infinitely large or small in magnitude.
57. Which of the following package stores all the simple data types in java?
a) lang
b) java
c) util
d) java.packages
Answer : a
Explanation : None.
58. What is the output of this program?
1. class isinfinite_output {
2. public static void main(String args[]) {
3. Double d = new Double(1 / 0.);
4. boolean x = d.isInfinite();
5. System.out.print(x);
6. }
7. }
a) 0
b) 1
c) true
d) false
Answer : c
Explanation : isInfinite() method returns true is the value being tested is infinitely large or small in magnitude.
1/0. is infinitely large in magnitude hence true is stored in x.
Output:
$ javac isinfinite_output.java
$ java isinfinite_output
true
59. What is the output of this program?
1. class isNaN_output {
2. public static void main(String args[]) {
3. Double d = new Double(1 / 0.);
4. boolean x = d.isNaN();
5. System.out.print(x);
6. }
7. }
a) 0
b) 1
c) true
d) false
Answer : d
Explanation : isisNaN() method returns true is the value being tested is a number. 1/0. is infinitely large in magnitude,
which cant not be defined as a number hence false is stored in x.
Output:
$ javac isNaN_output.java
$ java isNaN_output
false
60. What is the output of this program?
1. class binary {
2. public static void main(String args[]) {
3. int num = 17;
4. System.out.print(Integer.toBinaryString(num));
5. }
6. }
a) 1001
b) 10011
c) 11011
d) 10001
Answer : d
Explanation : None.
output:
$ javac binary.java
$ java binary
10001
61. Which of these is a wrapper for data type int?
a) Integer
b) Long
c) Byte
d) Double
Answer : a
Explanation : None.
62. Which of the following methods is a method of wrapper Integer for obtaining hash code for the invoking object?
a) int hash()
b) int hashcode()
c) int hashCode()
d) Integer hashcode()
Answer : c
Explanation : None.
63. Which of these is a super class of wrappers Long, Character & Integer?
a) Long
b) Digits
c) Float
d) Number
Answer : d
Explanation : Number is an abstract class containing subclasses Double, Float, Byte, Short, Integer and Long.
64. Which of these is wrapper for simple data type char?
a) Float
b) Character
c) String
d) Integer
Answer : b
Explanation : None.
65. Which of the following is method of wrapper Integer for converting the value of an object into byte?
a) bytevalue()
b) byte bytevalue()
c) Bytevalue()
d) Byte Bytevalue().
Answer : b
Explanation : None.
66. Which of these methods is used to obtain value of invoking object as a long?
a) long value()
b) long longValue()
c) Long longvalue()
d) Long Longvalue()
Answer : b
Explanation : long longValue() is used to obtain value of invoking object as a long.
67. What is the output of this program?
1. class Output {
2. public static void main(String args[]) {
3. char a[] = {'a', '5', 'A', ' '};
4. System.out.print(Character.isDigit(a[0]) + " ");
5. System.out.print(Character.isWhitespace(a[3]) + " ");
6. System.out.print(Character.isUpperCase(a[2]));
7. }
8. }
a) true false true
b) false true true
c) true true false
d) false false false
Answer : b
Explanation: Character.isDigit(a[0]) checks for a[0], whether it is a digit or not, since a[0] i:e ‘a’ is a character false is returned. a[3] is a whitespace
hence Character.isWhitespace(a[3]) returns a true. a[2] is an upper case letter i:e ‘A’ hence Character.isUpperCase(a[2]) returns true.
Output:
$ javac Output.java
$ java Output
false true true
68. What is the output of this program?
1. class Output {
2. public static void main(String args[]) {
3. Integer i = new Integer(257);
4. byte x = i.byteValue();
5. System.out.print(x);
6. }
7. }
a) 0
b) 1
c) 256
d) 257
Answer : b
Explanation : i.byteValue() method returns the value of wrapper i as a byte value. i is 257, range of byte is 256 therefore i value exceeds
byte range by 1 hence 1 is returned and stored in x.
Output:
$ javac Output.java
$ java Output
1
69. What is the output of this program?
1. class Output {
2. public static void main(String args[]) {
3. Integer i = new Integer(257);
4. float x = i.floatValue();
5. System.out.print(x);
6. }
7. }
a) 0
b) 1
c) 257
d) 257.0
Answer : d
Explanation : None.
Output:
$ javac Output.java
$ java Output
257.0
70. What is the output of this program?
1. class Output {
2. public static void main(String args[]) {
3. Long i = new Long(256);
4. System.out.print(i.hashCode());
5. }
6. }
a) 256
b) 256.0
c) 256.00
d) 257.00
Answer : a
Explanation: None.
Output:
$ javac Output.java
$ java Output
256
71. Which of these class have only one field ‘TYPE’?
a) Void
b) Process
c) System
d) Runtime
Answer : a
Explanation : The Void class has one field, TYPE, which holds a reference to the Class object for the type void.
72. Which of the following method of Process class can terminate a process?
a) void kill()
b) void destroy()
c) void terminate()
d) void exit()
Answer : b
Explanation : Kills the subprocess. The subprocess represented by this Process object is forcibly terminated.
73. Standard output variable ‘out’ is defined in which class?
a) Void
b) Process
c) Runtime
d) System
Answer : d
Explanation : Standard output variable ‘out’ is defined in System class. out is usually used in print statement i:e System.out.print().
74. Which of these class can encapsulate an entire executing program?
a) Void
b) Process
c) Runtime
d) System
Answer : b
Explanation : None.
75. Which of the following is method of System class is used to find how long a program takes to execute?
a) currenttime()
b) currentTime()
c) currentTimeMillis()
d) currenttimeMillis()
Answer : c
Explanation : None.
76. Which of these class holds a collection of static methods and variables?
a) Void
b) Process
c) Runtime
d) System
Answer : d
Explanation : System class holds a collection of static methods and variables. The standard input, output and error output of
java run time are stored in the in, out and err variables of System class.
77. What is the output of this program?
1. class Output {
2. public static void main(String args[]) {
3. long start, end;
4. start = System.currentTimeMillis();
5. for (int i = 0; i < 10000000; i++);
6. end = System.currentTimeMillis();
7. System.out.print(end - start);
8. }
9. }
a) 0
b) 1
c) 1000
d) System Dependent
Answer : d
Explanation : end time is the time taken by loop to execute it can be any non zero value depending on the System.
Output:
$ javac Output.java
$ java Output
78
78. What is the output of this program?
1. class Output {
2. public static void main(String args[]) {
3. byte a[] = { 65, 66, 67, 68, 69, 70 };
4. byte b[] = { 71, 72, 73, 74, 75, 76 };
5. System.arraycopy(a , 0, b, 0, a.length);
6. System.out.print(new String(a) + " " + new String(b));
7. }
8. }
a) ABCDEF ABCDEF
b) ABCDEF GHIJKL
c) GHIJKL ABCDEF
d) GHIJKL GHIJKL
Answer : a
Explanation : System.arraycopy() is a method of class System which is used to copy a string into another string.
Output:
$ javac Output.java
$ java Output
ABCDEF ABCDEF
79. What is the output of this program?
1. class Output {
2. public static void main(String args[]) {
3. byte a[] = { 65, 66, 67, 68, 69, 70 };
4. byte b[] = { 71, 72, 73, 74, 75, 76 };
5. System.arraycopy(a, 2, b, 1, a.length-2);
6. System.out.print(new String(a) + " " + new String(b));
7. }
8. }
a) ABCDEF GHIJKL
b) ABCDEF GCDEFL
c) GHIJKL ABCDEF
d) GCDEFL GHIJKL
Answer : b
Explanation : None.
Output:
$ javac Output.java
$ java Output
ABCDEF GCDEFL
80. What is the output of this program?
1. class Output {
2. public static void main(String args[]) {
3. byte a[] = { 65, 66, 67, 68, 69, 70 };
4. byte b[] = { 71, 72, 73, 74, 75, 76 };
5. System.arraycopy(a, 1, b, 3, 0);
6. System.out.print(new String(a) + " " + new String(b));
7. }
8. }
a) ABCDEF GHIJKL
b) ABCDEF GCDEFL
c) GHIJKL ABCDEF
d) GCDEFL GHIJKL
Answer : a
Explanation : Since last parameter of System.arraycopy(a,1,b,3,0) is 0 nothing is copied from array a to array b, hence b remains as it is.
Output:
$ javac Output.java
$ java Output
ABCDEF GHIJKL
81. Which of these packages contain classes and interfaces used for input & output operations of a program?
a) java.util
b) java.lang
c) java.io
d) All of the mentioned
Answer: c
Explanation: java.io provides support for input and output operations.
82. Which of these class is not a member class of java.io package?
a) String
b) StringReader
c) Writer
d) File
Answer: a
Explanation: None.
83. Which of these interface is not a member of java.io package?
a) DataInput
b) ObjectInput
c) ObjectFilter
d) FileFilter
Answer: c
Explanation: None.
84. Which of these class is not related to input and output stream in terms of functioning?
a) File
b) Writer
c) InputStream
d) Reader
Answer: a
Explanation: A File describes properties of a file, a File object is used to obtain or manipulate the information associated with a disk file, such as the permissions, time date, and directories path, and to navigate subdirectories.
85. Which of these is specified by a File object?
a) a file in disk
b) directory path
c) directory in disk
d) None of the mentioned
Answer: c
Explanation: None.
86. Which of these is method for testing whether the specified element is a file or a directory?
a) IsFile()
b) isFile()
c) Isfile()
d) isfile()
Answer: b
Explanation: isFile() returns true if called on a file and returns false when called on a directory.
87. What is the output of this program?
1. import java.io.*;
2. class files {
3. public static void main(String args[]) {
4. File obj = new File("/java/system");
5. System.out.print(obj.getName());
6. }
7. }
a) java
b) system
c) java/system
d) /java/system
Answer: b
Explanation: obj.getName() returns the name of the file.
Output:
$ javac files.java
$ java files
system
88. What is the output of this program?
1. import java.io.*;
2. class files {
3. public static void main(String args[]) {
4. File obj = new File("/java/system");
5. System.out.print(obj.getAbsolutePath());
6. }
7. }
Note: file is made in c drive.
a) java
b) system
c) java/system
d) /java/system
Answer: d
Explanation: None.
Output:
$ javac files.java
$ java files
\java\system
89. What is the output of this program?
1. import java.io.*;
2. class files {
3. public static void main(String args[]) {
4. File obj = new File("/java/system");
5. System.out.print(obj.canWrite());
6. System.out.print(" " + obj.canRead());
7. }
8. }
Note: file is made in c drive.
a) true false
b) false true
c) true true
d) false false
Answer: d
Explanation: None.
Output:
$ javac files.java
$ java files
false false
90. What is the output of this program?
1. import java.io.*;
2. class files {
3. public static void main(String args[]) {
4. File obj = new File("/java/system");
5. System.out.print(obj.getParent());
6. System.out.print(" " + obj.isFile());
7. }
8. }
Note: file is made in c drive.
a) java true
b) java false
c) \java false
d) \java true
Answer: c
Explanation: getparent() giver the parent directory of the file and isfile() checks weather the present file is a directory or a file in the disk
Output:
$ javac files.java
$ java files
\java false
91. Which of these classes is used for input and output operation when working with bytes?
a) InputStream
b) Reader
c) Writer
d) All of the mentioned
Answer: a
Explanation: InputStream & OutputStream are designed for byte stream. Reader and writer are designed for character stream.
92. Which of these class is used to read and write bytes in a file?
a) FileReader
b) FileWriter
c) FileInputStream
d) InputStreamReader
Answer: c
Explanation: None.
93. Which of these method of InputStream is used to read integer representation of next available byte input?
a) read()
b) scanf()
c) get()
d) getInteger()
Answer: a
Explanation: None.
94. Which of these data type is returned by every method of OutputStream?
a) int
b) float
c) byte
d) None of the mentioned
Answer: d
Explanation: Every method of OutputStream returns void and throws an IOExeption in case of errors.
95. Which of these is a method to clear all the data present in output buffers?
a) clear()
b) flush()
c) fflush()
d) close()
Answer: b
Explanation: None.
96. Which of these is method is used for writing bytes to an outputstream?
a) put()
b) print()
c) printf()
d) write()
Answer: b
Explanation: write() and read() are the two main methods of OutputStream & InputStream respectively that are used for printing and reading the byte data.
97. What is the output of this program?
1. import java.io.*;
2. class filesinputoutput {
3. public static void main(String args[]) {
4. InputStream obj = new FileInputStream("inputoutput.java");
5. System.out.print(obj.available());
6. }
7. }
Note: inputoutput.java is stored in the disk.
a) true
b) false
c) prints number of bytes in file
d) prints number of characters in the file
Answer: c
Explanation: obj.available() returns the number of bytes.
Output:
$ javac filesinputoutput.java
$ java filesinputoutput
1422
(Output will be different in your case)
98. What is the output of this program?
1. import java.io.*;
2. public class filesinputoutput {
3. public static void main(String[] args) {
4. String obj = "abc";
5. byte b[] = obj.getBytes();
6. ByteArrayInputStream obj1 = new ByteArrayInputStream(b);
7. for (int i = 0; i < 2; ++ i) {
8. int c;
9. while ((c = obj1.read()) != -1) {
10. if(i == 0) {
11. System.out.print((char)c);
12. }
13. }
14. }
15. }
16. }
a) abc
b) ABC
c) ab
d) AB
Answer: a
Explanation: None.
Output:
$ javac filesinputoutput.java
$ java filesinputoutput
abc
99. What is the output of this program?
1. import java.io.*;
2. public class filesinputoutput {
3. public static void main(String[] args) {
4. String obj = "abc";
5. byte b[] = obj.getBytes();
6. ByteArrayInputStream obj1 = new ByteArrayInputStream(b);
7. for (int i = 0; i < 2; ++ i) {
8. int c;
9. while ((c = obj1.read()) != -1) {
10. if (i == 0) {
11. System.out.print(Character.toUpperCase((char)c));
12. }
13. }
14. }
15. }
16. }
a) abc
b) ABC
c) ab
d) AB
Answer: b
Explanation: None.
Output:
$ javac filesinputoutput.java
$ java filesinputoutput
ABC
100. What is the output of this program?
1. import java.io.*;
2. public class filesinputoutput {
3. public static void main(String[] args) {
4. String obj = "abc";
5. byte b[] = obj.getBytes();
6. ByteArrayInputStream obj1 = new ByteArrayInputStream(b);
7. for (int i = 0; i < 2; ++ i) {
8. int c;
9. while ((c = obj1.read()) != -1) {
10. if (i == 0) {
11. System.out.print(Character.toUpperCase((char)c));
12. obj2.write(1);
13. }
14. }
15. System.out.print(obj2);
16. }
17. }
18. }
a) AaBaCa
b) ABCaaa
c) AaaBaaCaa
d) AaBaaCaaa
Answer: d
Explanation: None.
Output:
$ javac filesinputoutput.java
$ java filesinputoutput
AaBaaCaaa
101. Which of these is a process of writing the state of an object to a byte stream?
a) Serialization
b) Externalization
c) File Filtering
d) All of the mentioned
Answer: a
Explanation: Serialization is the process of writing the state of an object to a byte stream. This is used when you want to save the state of your program to persistent storage area.
102. Which of these process occur automatically by java run time system?
a) Serialization
b) Garbage collection
c) File Filtering
d) All of the mentioned
Answer: a
Explanation: Serialization and deserialization occur automatically by java run time system, Garbage collection also occur automatically but is done by CPU or the operating system not by the java run time system.
103. Which of these is an interface for control over serialization and deserialization?
a) Serializable
b) Externalization
c) FileFilter
d) ObjectInput
Answer: b
Explanation: None.
104. Which of these interface extends DataOutput interface?
a) Serializable
b) Externalization
c) ObjectOutput
d) ObjectInput
Answer: c
Explanation: ObjectOutput interface extends the DataOutput interface and supports object serialization.
105. Which of these is a method of ObjectOutput interface used to finalize the output state so that any buffers are cleared?
a) clear()
b) flush()
c) fflush()
d) close()
Answer: b
Explanation: None.
106. Which of these is method of ObjectOutput interface used to write the object to input or output stream as required?
a) write()
b) Write()
c) StreamWrite()
d) writeObject()
Answer: d
Explanation: writeObject() is used to write an object into invoking stream, it can be input stream or output stream.
107. What is the output of this program?
1. import java.io.*;
2. class serialization {
3. public static void main(String[] args) {
4. try {
5. Myclass object1 = new Myclass("Hello", -7, 2.1e10);
6. FileOutputStream fos = new FileOutputStream("serial");
7. ObjectOutputStream oos = new ObjectOutputStream(fos);
8. oos.writeObject(object1);
9. oos.flush();
10. oos.close();
11. }
12. catch(Exception e) {
13. System.out.println("Serialization" + e);
14. System.exit(0);
15. }
16. try {
17. Myclass object2;
18. FileInputStream fis = new FileInputStream("serial");
19. ObjectInputStream ois = new ObjectInputStream(fis);
20. object2 = (Myclass)ois.readObject();
21. ois.close();
22. System.out.println(object2);
23. }
24. catch (Exception e) {
25. System.out.print("deserialization" + e);
26. System.exit(0);
27. }
28. }
29. }
30. class Myclass implements Serializable {
31. String s;
32. int i;
33. double d;
34. Myclass (String s, int i, double d){
35. this.d = d;
36. this.i = i;
37. this.s = s;
38. }
39. }
a) s=Hello; i=-7; d=2.1E10
b) Hello; -7; 2.1E10
c) s; i; 2.1E10
d) Serialization
Answer: a
Explanation: None.
Output:
$ javac serialization.java
$ java serialization
s=Hello; i=-7; d=2.1E10
108. What is the output of this program?
1. import java.io.*;
2. class serialization {
3. public static void main(String[] args) {
4. try {
5. Myclass object1 = new Myclass("Hello", -7, 2.1e10);
6. FileOutputStream fos = new FileOutputStream("serial");
7. ObjectOutputStream oos = new ObjectOutputStream(fos);
8. oos.writeObject(object1);
9. oos.flush();
10. oos.close();
11. }
12. catch(Exception e) {
13. System.out.println("Serialization" + e);
14. System.exit(0);
15. }
16. try {
17. int x;
18. FileInputStream fis = new FileInputStream("serial");
19. ObjectInputStream ois = new ObjectInputStream(fis);
20. x = ois.readInt();
21. ois.close();
22. System.out.println(x);
23. }
24. catch (Exception e) {
25. System.out.print("deserialization");
26. System.exit(0);
27. }
28. }
29. }
30. class Myclass implements Serializable {
31. String s;
32. int i;
33. double d;
34. Myclass(String s, int i, double d){
35. this.d = d;
36. this.i = i;
37. this.s = s;
38. }
39. }
a) -7
b) Hello
c) 2.1E10
d) deserialization
Answer: d
Explanation: x = ois.readInt(); will try to read an integer value from the stream ‘serial’ created before, since stream contains an object of Myclass hence error will occur and it will be catched by catch printing deserialization.
Output:
$ javac serialization.java
$ java serialization
deserialization
109. What is the output of this program?
1. import java.io.*;
2. class Chararrayinput {
3. public static void main(String[] args) {
4. String obj = "abcdefgh";
5. int length = obj.length();
6. char c[] = new char[length];
7. obj.getChars(0, length, c, 0);
8. CharArrayReader input1 = new CharArrayReader(c);
9. CharArrayReader input2 = new CharArrayReader(c, 1, 4);
10. int i;
11. int j;
12. try {
13. while ((i = input1.read()) == (j = input2.read())) {
14. System.out.print((char)i);
15. }
16. }
17. catch (IOException e) {
18. e.printStackTrace();
19. }
20. }
21. }
a) abc
b) abcd
c) abcde
d) None of the mentioned
Answer: d
Explanation: No output is printed. CharArrayReader object input1 contains string “abcdefgh” whereas object input2 contains string “bcde”, when while((i=input1.read())==(j=input2.read())) is executed the starting character of each object is compared since they are unequal control comes out of loop and nothing is printed on the screen.
Output:
$ javac Chararrayinput.java
$ java Chararrayinput
110. What is the output of this program?
1. import java.io.*;
2. class streams {
3. public static void main(String[] args) {
4. try {
5. FileOutputStream fos = new FileOutputStream("serial");
6. ObjectOutputStream oos = new ObjectOutputStream(fos);
7. oos.writeFloat(3.5);
8. oos.flush();
9. oos.close();
10. }
11. catch(Exception e) {
12. System.out.println("Serialization" + e);
13. System.exit(0);
14. }
15. try {
16. float x;
17. FileInputStream fis = new FileInputStream("serial");
18. ObjectInputStream ois = new ObjectInputStream(fis);
19. x = ois.readInt();
20. ois.close();
21. System.out.println(x);
22. }
23. catch (Exception e) {
24. System.out.print("deserialization");
25. System.exit(0);
26. }
27. }
28. }
a) 3
b) 3.5
c) serialization
d) deserialization
Answer: b
Explanation: oos.writeFloat(3.5); writes in output stream which is extracted by x = ois.readInt(); and stored in x hence x contains 3.5.
Output:
$ javac streams.java
$ java streams
3.5
111. Which of these is a process of extracting/removing the state of an object from a stream?
a) Serialization
b) Externalization
c) File Filtering
d) Deserialization
Answer: d
Explanation: Deserialization is a process by which the data written in the stream can be extracted out from the stream.
112. Which of these process occur automatically by java run time system?
a) Serialization
b) Memory allocation
c) Deserialization
d) All of the mentioned
Answer: d
Explanation: Serialization, deserialization and Memory allocation occur automatically by java run time system.
113. Which of these is an interface for control over serialization and deserialization?
a) Serializable
b) Externalization
c) FileFilter
d) ObjectInput
Answer: b
Explanation: None.
114. Which of these interface extends DataInput interface?
a) Serializable
b) Externalization
c) ObjectOutput
d) ObjectInput
Answer: d
Explanation: ObjectInput interface extends the DataInput interface and supports object serialization.
115. Which of these is a method of ObjectInput interface used to deserialize an object from a stream?
a) int read()
b) void close()
c) Object readObject()
d) Object WriteObject()
Answer: c
Explanation: None.
116. Which of these class extend InputStream class?
a) ObjectStream
b) ObjectInputStream
c) ObjectOutput
d) ObjectInput
Answer: b
Explanation: ObjectInputStream class extends the InputStream class and implements the ObjectInput interface.
117. What is the output of this program?
1. import java.io.*;
2. class streams {
3. public static void main(String[] args) {
4. try {
5. FileOutputStream fos = new FileOutputStream("serial");
6. ObjectOutputStream oos = new ObjectOutputStream(fos);
7. oos.writeInt(5);
8. oos.flush();
9. oos.close();
10. }
11. catch(Exception e) {
12. System.out.println("Serialization" + e);
13. System.exit(0);
14. }
15. try {
16. int z;
17. FileInputStream fis = new FileInputStream("serial");
18. ObjectInputStream ois = new ObjectInputStream(fis);
19. z = ois.readInt();
20. ois.close();
21. System.out.println(x);
22. }
23. catch (Exception e) {
24. System.out.print("deserialization");
25. System.exit(0);
26. }
27. }
28. }
a) 5
b) void
c) serialization
d) deserialization
Answer: a
Explanation: oos.writeInt(5); writes integer 5 in the Output stream which is extracted by z = ois.readInt(); and stored in z hence z contains 5.
Output:
$ javac streams.java
$ java streams
5
118. What is the output of this program?
1. import java.io.*;
2. class serialization {
3. public static void main(String[] args) {
4. try {
5. Myclass object1 = new Myclass("Hello", -7, 2.1e10);
6. FileOutputStream fos = new FileOutputStream("serial");
7. ObjectOutputStream oos = new ObjectOutputStream(fos);
8. oos.writeObject(object1);
9. oos.flush();
10. oos.close();
11. }
12. catch(Exception e) {
13. System.out.println("Serialization" + e);
14. System.exit(0);
15. }
16. try {
17. int x;
18. FileInputStream fis = new FileInputStream("serial");
19. ObjectInputStream ois = new ObjectInputStream(fis);
20. x = ois.readInt();
21. ois.close();
22. System.out.println(x);
23. }
24. catch (Exception e) {
25. System.out.print("deserialization");
26. System.exit(0);
27. }
28. }
29. }
30. class Myclass implements Serializable {
31. String s;
32. int i;
33. double d;
34. Myclass(String s, int i, double d){
35. this.d = d;
36. this.i = i;
37. this.s = s;
38. }
39. }
a) -7
b) Hello
c) 2.1E10
d) deserialization
Answer: d
Explanation: x = ois.readInt(); will try to read an integer value from the stream ‘serial’ created before, since stream contains an object of Myclass hence error will occur and it will be catched by catch printing deserialization.
Output:
$ javac serialization.java
$ java serialization
deserialization
119. What is the output of this program?
1. import java.io.*;
2. class streams {
3. public static void main(String[] args) {
4. try {
5. FileOutputStream fos = new FileOutputStream("serial");
6. ObjectOutputStream oos = new ObjectOutputStream(fos);
7. oos.writeFloat(3.5);
8. oos.flush();
9. oos.close();
10. }
11. catch(Exception e) {
12. System.out.println("Serialization" + e);
13. System.exit(0);
14. }
15. try {
16. FileInputStream fis = new FileInputStream("serial");
17. ObjectInputStream ois = new ObjectInputStream(fis);
18. ois.close();
19. System.out.println(ois.available());
20. }
21. catch (Exception e) {
22. System.out.print("deserialization");
23. System.exit(0);
24. }
25. }
26. }
a) 1
b) 2
c) 3
d) 4
Answer: d
Explanation: New input stream is linked to streal ‘serials’, an object ‘ois’ of ObjectInputStream is used to access this newly created stream, ois.close(); closes the stream hence we can’t access the stream and ois.available() returns 0.
Output:
$ javac streams.java
$ java streams
0
120. What is the output of this program?
1. import java.io.*;
2. class streams {
3. public static void main(String[] args) {
4. try {
5. FileOutputStream fos = new FileOutputStream("serial");
6. ObjectOutputStream oos = new ObjectOutputStream(fos);
7. oos.writeFloat(3.5);
8. oos.flush();
9. oos.close();
10. }
11. catch(Exception e) {
12. System.out.println("Serialization" + e);
13. System.exit(0);
14. }
15. try {
16. FileInputStream fis = new FileInputStream("serial");
17. ObjectInputStream ois = new ObjectInputStream(fis);
18. System.out.println(ois.available());
19. }
20. catch (Exception e) {
21. System.out.print("deserialization");
22. System.exit(0);
23. }
24. }
25. }
a) 1
b) 2
c) 3
d) 4
Answer: D
Explanation: oos.writeFloat(3.5); writes 3.5 in output stream. A new input stream is linked to stream ‘serials’, an object ‘ois’ of ObjectInputStream is used to access this newly created stream, ois.available() gives the total number of byte in the input stream since a float was written in the stream thus the stream contains 4 byte, hence 4 is returned and printed.
Output:
$ javac streams.java
$ java streams
4
No comments:
Post a Comment