Java Quiz-1

Reviewed by Editorial Team
The ProProfs editorial team is comprised of experienced subject matter experts. They've collectively created over 10,000 quizzes and lessons, serving over 100 million users. Our team includes in-house content moderators and subject matter experts, as well as a global network of rigorously trained contributors. All adhere to our comprehensive editorial guidelines, ensuring the delivery of high-quality content.
Learn about Our Editorial Process
| By Rahul Kumar
R
Rahul Kumar
Community Contributor
Quizzes Created: 3 | Total Attempts: 11,103
| Attempts: 5,450 | Questions: 100
Please wait...
Question 1 / 100
0 %
0/100
Score 0/100
1. Class A, B and C are in multilevel inheritance hierarchy repectively . In the main method of some other class if class C object is created, in what sequence the three constructors execute? 1.Constructor of A executes first, followed by the constructor of B and C 2.Constructor of C executes first followed by the constructor of A and B 3.Constructor of C executes first followed by the constructor of B and A 4.Constructor of A executes first followed by the constructor of C and B

Explanation

In a multilevel inheritance hierarchy, the constructors of the parent classes are executed first before the constructor of the child class. In this case, since class C is the child class of class B and class B is the child class of class A, the constructor of class A will execute first, followed by the constructor of class B, and finally the constructor of class C. Therefore, option 1 is the correct answer.

Submit
Please wait...
About This Quiz
Java Quiz-1 - Quiz

Java Quiz-1 assesses foundational Java programming skills through multiple-choice questions. Topics include constructor behaviors, method definitions, and class hierarchies. Ideal for enhancing understanding of Java's core concepts.

2. What will happen when you attempt to compile and run this code? abstract class Base{ abstract public void myfunc(); public void another(){ System.out.println("Another method"); } } public class Abs extends Base{ public static void main(String argv[]){ Abs a = new Abs(); a.amethod(); } public void myfunc(){ System.out.println("My Func"); } public void amethod(){ myfunc(); } } 1.The code will compile and run, printing out the words "My Func" 2.The compiler will complain that the Base class has non abstract methods 3.The code will compile but complain at run time that the Base class has non abstract methods 4.The compiler will complain that the method myfunc in the base class has no body, nobody at all to print it

Explanation

The code will compile and run without any errors, and it will print out the words "My Func". This is because the class Abs extends the abstract class Base and provides an implementation for the abstract method myfunc(). The amethod() in the Abs class calls the myfunc() method, which will print out "My Func" when executed.

Submit
3. If no retention policy is specified for an annotation, then the default policy of __________ is used. method class source runtime

Explanation

If no retention policy is specified for an annotation, then the default policy of "class" is used.

Submit
4. Package QB; class Sphere { protected int methodRadius(int r) { System.out.println("Radious is: "+r); return 0; } } package QB; public class MyClass { public static void main(String[] args) { double x = 0.89; Sphere sp = new Sphere(); // Some code missing } } to get the radius value what is the code of line to be added ? methodRadius(x); sp.methodRadius(x); Nothing to add Sphere.methodRadius();

Explanation

The code to be added to get the radius value is "sp.methodRadius(x);" This line of code calls the method "methodRadius" from the "Sphere" class and passes the value of "x" as a parameter, which will print the radius value.

Submit
5. Class Test{ public static void main(String[] args) { int x=-1,y=-1; if(++x=++y) System.out.println("R.T. Ponting"); else System.out.println("C.H. Gayle"); } } consider the code above & select the proper output from the options. R.T.Ponting C.H.Gayle Compile error none of the listed options

Explanation

The code will result in a compile error because the expression "++x=++y" is not a valid assignment. The "++" operator can only be used on a variable, not on an expression.

Submit
6. Class MyClass1 { private int area(int side) { return(side * side); } public static void main(String args[ ]) { MyClass1 MC = new MyClass1( ); int area = MC.area(50); System.out.println(area); } } What would be the output? Compilation error Runtime Exception 2500 50

Explanation

The output of the given code would be 2500. This is because the code defines a class MyClass1 with a private method area that calculates the area of a square given its side length. In the main method, an object of MyClass1 is created and the area method is called with an argument of 50. The calculated area is then printed, which is 2500.

Submit
7. Consider the following code and choose the correct option: class Test{ public static void main(String args[]){ TreeSet ts=new TreeSet(); ts.add(1); ts.add(8); ts.add(6); ts.add(4); SortedSet ss=ts.subSet(2, 10); ss.add(9); System.out.println(ts); System.out.println(ss); }} [1,4,6,8] [4,6,8,9] [1,8,6,4] [8,6,4,9] [1,4,6,8,9] [4,6,8,9] [1,4,6,8,9] [4,6,8]

Explanation

The code creates a TreeSet object and adds four elements to it: 1, 8, 6, and 4. Then, it creates a SortedSet object called ss using the subSet method of the TreeSet, specifying the range from 2 to 10. The add method is called on ss to add the element 9. Finally, the code prints the contents of both ts and ss. Since ts is not modified after creating ss, it remains [1, 4, 6, 8]. However, ss is modified and contains [4, 6, 8, 9]. Therefore, the correct answer is Option 3.

Submit
8. Which modifier is used to control access to critical code in multi-threaded programs? default public transient synchronized

Explanation

The modifier "synchronized" is used to control access to critical code in multi-threaded programs. This modifier ensures that only one thread can access the synchronized code block at a time, preventing multiple threads from accessing it simultaneously and causing potential conflicts or data corruption.

Submit
9. A) A call to instance method can not be made from static context. B) A call to static method can be made from non static context. 1.Both are FALSE 2.Both are TRUE 3.Only A is TRUE 4.Only B is TRUE

Explanation

The correct answer is Option 2 because both statements A and B are true. A call to an instance method cannot be made from a static context, as instance methods belong to specific objects and can only be accessed through an instance of the class. On the other hand, a call to a static method can be made from a non-static context, as static methods belong to the class itself and can be accessed without creating an instance of the class.

Submit
10. Public class MyClass { static void print(String s, int i) { System.out.println("String: " + s + ", int: " + i); } static void print(int i, String s) { System.out.println("int: " + i + ", String: " + s); } public static void main(String[] args) { print("String first", 11); print(99, "Int first"); } } What would be the output? 1.String: String first, int: 11 int: 99, String: Int first 2.int: 27, String: Int first String: String first, int: 27 3.Compilation Error 4.Runtime Exception

Explanation

The output would be "String: String first, int: 11 int: 99, String: Int first". This is because the print method is overloaded with two different parameter orders: print(String s, int i) and print(int i, String s). In the main method, the first print statement matches the print(String s, int i) method, so it prints "String: String first, int: 11". The second print statement matches the print(int i, String s) method, so it prints "int: 99, String: Int first".

Submit
11. What will be the result when you attempt to compile this program? public class Rand{ public static void main(String argv[]){ int iRand; iRand = Math.random(); System.out.println(iRand); } } Compile time error referring to a cast problem A random number between 1 and 10 A random number between 0 and 1 A compile time error as random being an undefined method

Explanation

The program will result in a compile time error referring to a cast problem. This is because the Math.random() method returns a double value between 0.0 and 1.0, but the variable iRand is of type int. Therefore, there is a type mismatch and a cast is required to convert the double value to an int. The program does not include this cast, resulting in a compile time error.

Submit
12. Which of the following will print -4.0 1.System.out.println(Math.ceil(-4.7)); 2.System.out.println(Math.floor(-4.7)); 3.System.out.println(Math.round(-4.7)); 4.System.out.println(Math.min(-4.7));

Explanation

Math.ceil(-4.7) returns the smallest integer that is greater than or equal to -4.7, which is -4. Therefore, when System.out.println is used to print Math.ceil(-4.7), it will print -4.0.

Submit
13. Custom annotations can be created using @interface @inherit @include all the listed options

Explanation

Custom annotations can be created using the "@interface" keyword. The "@interface" keyword is used in Java to define a new annotation type. Annotations provide metadata about a program, and custom annotations can be used to add additional information or behavior to code. By using the "@interface" keyword, developers can create their own custom annotations with specific properties and methods.

Submit
14. What will happen if a main() method of a "testing" class tries to access a private instance variable of an object using dot notation? 1.The compiler will automatically change the private variable to a public variable 2.The compiler will find the error and will not make a .class file 3.The program will compile and run successfully 4.The program will compile successfully, but the .class file will not run correctly

Explanation

If a main() method of a "testing" class tries to access a private instance variable of an object using dot notation, the compiler will find the error and will not make a .class file. This is because private instance variables can only be accessed within the same class and not from outside the class, even if it is the main method. Therefore, the compiler will detect this violation of access control and generate an error, preventing the creation of the .class file.

Submit
15. Class Order{ Order(){ System.out.println("Cat"); } public static void main(String... Args){ Order obj = new Order(); System.out.println("Ant"); } static{ System.out.println("Dog"); }} consider the code above & select the proper output from the options. Cat Ant Dog Dog Cat Ant Ant Cat Dog none

Explanation

The correct answer is Option 2 because the static block is executed first, so "Dog" is printed first. Then, the constructor is called, printing "Cat". Finally, in the main method, "Ant" is printed. Therefore, the correct output is "Dog Cat Ant".

Submit
16. Consider the following code and choose the correct option: class Test{ private static void display(){ System.out.println("Display()");} private static void show() { display(); System.out.println("show()");} public static void main(String arg[]){ show();}} 1.Compiles and prints show() 2.Compiles and prints Display() show() 3.Compiles but throws runtime exception 4.Compilation error

Explanation

The code will compile and print "Display()" followed by "show()". This is because the main method calls the show() method, which in turn calls the display() method. The display() method prints "Display()" and then the show() method prints "show()". Therefore, the correct option is Option 2.

Submit
17. Consider the following code and choose the correct option: package aj; class S{ int roll =23; private S(){} } package aj; class T { public static void main(String ar[]){ System.out.print(new S().roll);}} Compilation error Compiles and display 0 Compiles and display 23 Compiles but no output

Explanation

The code will result in a compilation error because the constructor of class S is marked as private, making it inaccessible from class T. Therefore, the code will not be able to create an instance of class S and access the roll variable.

Submit
18. Class Sample {int a,b; Sample() { a=1; b=2; System.out.println(a+"\t"+b); } Sample(int x) { this(10,20); a=b=x; System.out.println(a+"\t"+b); } Sample(int a,int b) { this(); this.a=a; this.b=b; System.out.println(a+"\t"+b); } } class This2 { public static void main(String args[]) { Sample s1=new Sample (100); } } What is the Output of the Program? 1.100 100 1 2 10 20 2.1 2 100 100 10 20 3.10 20 1 2 100 100 4.1 2 10 20 100 100

Explanation

The correct answer is Option 4.

In the main method, a new object of the Sample class is created with the value 100 passed as a parameter. This calls the Sample(int x) constructor.

Inside the Sample(int x) constructor, the this(10,20) statement is called, which in turn calls the Sample(int a, int b) constructor.

Inside the Sample(int a, int b) constructor, the this() statement is called, which calls the Sample() constructor.

Inside the Sample() constructor, the values of a and b are set to 1 and 2 respectively and then printed.

After that, the values of a and b in the Sample(int a, int b) constructor are set to the values passed as parameters (10 and 20), and then printed.

Finally, the values of a and b in the Sample(int x) constructor are set to the value passed as a parameter (100), and then printed.

So the output of the program is:
1 2
10 20
100

Submit
19. Class One{ int var1; One (int x){ var1 = x; }} class Derived extends One{ int var2; void display(){ System.out.println("var 1="+var1+"var2="+var2); }} class Main{ public static void main(String[] args){ Derived obj = new Derived(); obj.display(); }} consider the code above & select the proper output from the options. 1.0 , 0 2.compiles successfully but runtime error 3.compile error 4.none of these

Explanation

The code will not compile because the class Derived does not have a constructor that matches the constructor in the superclass One.

Submit
20. Consider the following code and choose the correct option: class A{ private static void display(){ System.out.print("Hi");} public static void main(String ar[]){ display();}} 1.Compiles and display Hi 2.Compiles and throw run time exception 3.Compiles but doesn't display anything 4.Compilation fails

Explanation

The code will compile and display "Hi" because the display() method is called within the main() method. Since the display() method is private, it can only be accessed within the class A.

Submit
21. Consider the following code was executed on June 01, 1983. What will be the output? class Test{ public static void main(String args[]){ Date date=new Date(); SimpleDateFormat sd; sd=new SimplpeDateFormat("E MMM dd yyyy"); System.out.print(sd.format(date));}} Wed Jun 01 1983 244 JUN 01 1983 PST JUN 01 1983 GMT JUN 01 1983

Explanation

The code initializes a Date object and a SimpleDateFormat object. The SimpleDateFormat object is then set to the format "E MMM dd yyyy", which represents the day of the week, month, day, and year. The code then prints the formatted date using the format specified. Therefore, the output will be "Wed Jun 01 1983".

Submit
22. Class Order{ Order(){ System.out.println("Cat"); } public static void main(String... Args){ System.out.println("Ant"); } static{ System.out.println("Dog"); } { System.out.println("Man"); }} consider the code above & select the proper output from the options. Dog Ant Dog Man Cat Ant Man Dog Ant Dog Man Ant

Explanation

The code provided is a class named "Order" with a constructor and a main method. The static block is executed first, so "Dog" is printed. Then, the instance block is executed, so "Man" is printed. Next, the constructor is called, so "Cat" is printed. Finally, the main method is executed, so "Ant" is printed. Therefore, the correct output is "Dog Ant".

Submit
23. A constructor may return value including class type

Explanation

In object-oriented programming, a constructor is a special method that is used to initialize an object of a class. Constructors do not have a return type, including class type. They are responsible for setting the initial state of an object and are automatically called when an object is created. Therefore, the statement that a constructor may return a value, including class type, is false.

Submit
24. All annotation types should maually extend the Annotation interface.

Explanation

The statement is false because not all annotation types need to manually extend the Annotation interface. In Java, there are three types of annotations: marker annotations, single-value annotations, and full annotations. Marker annotations do not require the Annotation interface to be extended, as they do not have any elements. Single-value annotations can extend the Annotation interface, but it is not mandatory. Only full annotations, which have multiple elements, need to manually extend the Annotation interface.

Submit
25. Nt indexOf(Object o) - What does this method return if the element is not found in the List? null -1 0 none of the listed options

Explanation

The indexOf(Object o) method in Java returns -1 if the element is not found in the List. This is because the method searches for the specified element in the List and returns the index of the first occurrence of the element. If the element is not found, it returns -1 to indicate that the element does not exist in the List.

Submit
26. Given: 10. interface A { void x(); } 11. class B implements A { public void x() { } public void y() { } } 12. class C extends B { public void x() {} } And: 20. java.util.List<a> list = new java.util.ArrayList</a>(); 21. list.add(new B()); 22. list.add(new C()); 23. for (A a:list) { 24. a.x(); 25. a.y();; 26. } What is the result? Compilation fails because of an error in line 25 The code runs with no output. An exception is thrown at runtime Compilation fails because of an error in line 21

Explanation

The code will fail to compile because there is an error in line 25. The interface A does not have a method called y(), so calling a.y() is not valid.

Submit
27. Consider the following code and choose the correct option: public class MyClass { public static void main(String arguments[]) { amethod(arguments); } public void amethod(String[] arguments) { System.out.println(arguments[0]); System.out.println(arguments[1]); } } Command Line arguments - Hi, Hello prints Hi Hello Compiler Error Runs but no output Runtime Error

Explanation

The code is trying to print the command line arguments passed to the program. In the main method, the amethod is called with the "arguments" array as the parameter. In the amethod, the first and second elements of the "arguments" array are printed. Since the command line arguments are "Hi" and "Hello", the code will print "Hi" and "Hello" as the output. Therefore, Option 2 is the correct answer.

Submit
28. Consider the code below & select the correct ouput from the options: public class Test{ public static void main(String[] args) { String []colors={"orange","blue","red","green","ivory"}; Arrays.sort(colors); int s1=Arrays.binarySearch(colors, "ivory"); int s2=Arrays.binarySearch(colors, "silver"); System.out.println(s1+" "+s2); }} A. 2 -4 B. 3 -5 C. 2 -6 D. 3 -4

Explanation

The correct answer is Option 3. The code sorts the array of colors in alphabetical order using Arrays.sort(). Then, it uses Arrays.binarySearch() to find the index of the element "ivory" and "silver" in the sorted array. Since "ivory" is present in the array, its index is returned as 2. However, "silver" is not present in the array, so its index is returned as the bitwise complement of the index where it should be inserted. In this case, the index where "silver" should be inserted is 5, so its complement is -6. Therefore, the output is 2 -6.

Submit
29. Given: static void myFunc() { int i, s = 0; for (int j = 0; j < 7; j++) { i = 0; do { i++; s++; } while (i < j); } System.out.println(s); } } What would be the result 20 21 22 23

Explanation

The correct answer is Option 3. The code snippet contains a nested loop. The outer loop iterates from 0 to 6, and the inner loop increments the variable i until it reaches the value of j. The variable s is incremented every time the inner loop executes. So, for each iteration of the outer loop, the inner loop will execute j times, and s will be incremented by j. Therefore, the final value of s will be the sum of all the values of j from 0 to 6, which is 0 + 1 + 2 + 3 + 4 + 5 + 6 = 21.

Submit
30. Here is the general syntax for method definition: accessModifier returnType methodName( parameterList ) { Java statements return returnValue; } What is true for the returnType and the returnValue? 1.The returnValue can be any type, but will be automatically converted to returnType when the method returns to the caller 2.If the returnType is void then the returnValue can be any type 3.The returnValue must be the same type as the returnType, or be of a type that can be converted to returnType without loss of information 4.The returnValue must be exactly the same type as the returnType.

Explanation

The explanation for the correct answer (Option 3) is that the returnValue must be the same type as the returnType or be of a type that can be converted to returnType without loss of information. This means that the value returned by the method must either be of the same type as specified in the method's returnType or it can be a type that can be safely converted to the returnType without any loss of data.

Submit
31. Class A { int i, j; A(int a, int b) { i = a; j = b; } void show() { System.out.println("i and j: " + i + " " + j); } } class B extends A { int k; B(int a, int b, int c) { super(a, b); k = c; } void show(String msg) { System.out.println(msg + k); } } class Override { public static void main(String args[]) { B subOb = new B(3, 5, 7); subOb.show("This is k: "); // this calls show() in B subOb.show(); // this calls show() in A } } What would be the ouput? This is j: 5 i and k: 3 7 This is i: 3 j and k: 5 7 This is i: 7 j and k: 3 5 This is k: 7 i and j: 3 7

Explanation

The output would be "This is k: 7 i and j: 3 7". This is because the main method creates an object of class B and calls the show method with the message "This is k: ". This calls the show method in class B, which prints the value of k. Then, the main method calls the show method again without any arguments, which calls the show method in class A, which prints the values of i and j.

Submit
32. Package QB; class Meal { Meal() { System.out.println("Meal()"); } } class Cheese { Cheese() { System.out.println("Cheese()"); } } class Lunch extends Meal { Lunch() { System.out.println("Lunch()"); } } class PortableLunch extends Lunch { PortableLunch() { System.out.println("PortableLunch()"); } } class Sandwich extends PortableLunch { private Cheese c = new Cheese(); public Sandwich() { System.out.println("Sandwich()"); } } public class MyClass7 { public static void main(String[] args) { new Sandwich(); 1.Meal() Lunch() PortableLunc h() Cheese() Sandwich() 2.Meal() Cheese() Lunch() PortableLunc h() Sandwich() 3.Meal() Lunch() PortableLunc h() Sandwich() Cheese() 4.Cheese() Sandwich() Meal() Lunch() PortableLunc h()

Explanation

The correct answer is Option 1. The order of the constructors being called is as follows: Meal() -> Lunch() -> PortableLunch() -> Cheese() -> Sandwich(). The Sandwich class extends PortableLunch, which extends Lunch, which extends Meal. Therefore, the constructors of the superclass are called before the constructor of the subclass.

Submit
33. Class Order{ Order(){ System.out.println("Cat"); } public static void main(String... Args){ Order obj = new Order(); System.out.println("Ant"); } static{ System.out.println("Dog"); } { System.out.println("Man"); }} consider the code above & select the proper output from the options. Man Dog Cat Ant Cat Ant Dog Man Dog Man Cat Ant compile error

Explanation

The code is a class named "Order" with a constructor and a main method. The constructor is called when an object of the class is created, and it prints "Cat". The static block is executed before the main method and it prints "Dog". The instance block is executed before the constructor and it prints "Man". Finally, in the main method, an object of the class is created and it prints "Ant". Therefore, the correct output is "Dog Man Cat Ant", which corresponds to Option 3.

Submit
34. Given: public class Venus { public static void main(String[] args) { int [] x = {1,2,3}; int y[] = {4,5,6}; new Venus().go(x,y); } void go(int[]... z) { for(int[] a : z) System.out.print(a[0]); } } What is the result? 123 12 14 1

Explanation

The code creates an array 'x' with values {1,2,3} and an array 'y' with values {4,5,6}. The 'go' method takes in a variable number of int arrays as arguments. In the main method, the 'go' method is called with 'x' and 'y' as arguments. Inside the 'go' method, a for-each loop iterates over the arguments and prints the first element of each array. Therefore, the output will be "14".

Submit
35. Suppose class B is sub class of class A: A) If class A doesn't have any constructor, then class B also must not have any constructor B) If class A has parameterized constructor, then class B can have default as well as parameterized constructor C) If class A has parameterized constructor then call to class A constructor should be made explicitly by constructor of class B Only B and C is TRUE Only A is TRUE All are FALSE Only A and C is TRUE

Explanation

If class A doesn't have any constructor, it means that the default constructor is not defined in class A. In this case, class B can still have a default constructor or a parameterized constructor. This is because class B can inherit the default constructor from class A, if it exists, or it can define its own constructor. Therefore, option B is true. Option C is also true because if class A has a parameterized constructor, the constructor of class B needs to explicitly call the constructor of class A using the super() keyword.

Submit
36. Inorder to remove one element from the given Treeset, place the appropriate line of code public class Main { public static void main(String[] args) { TreeSet<Integer> tSet = new TreeSet<Integer>(); System.out.println("Size of TreeSet : " + tSet.size()); tSet.add(new Integer("1")); tSet.add(new Integer("2")); tSet.add(new Integer("3")); System.out.println(tSet.size()); // remove the one element from the Treeset System.out.println("Size of TreeSet after removal : " + tSet.size()); } } tSet.clear(new Integer("1")); tSetdelete(new Integer("1")); tSet.remove(new Integer("1")); tSet.drop(new Integer("1"));

Explanation

The appropriate line of code to remove one element from the given TreeSet is tSet.remove(new Integer("1")). This line of code uses the remove() method of the TreeSet class to remove the element with the value "1" from the TreeSet.

Submit
37. Consider the following code and choose the correct option: package aj; class A{ protected int j; } package bj; class B extends A { public static void main(String ar[]){ System.out.print(new A().j=23);}} 1.code compiles fine and will display 23 2.code compiles but will not display output 3.compliation error 4.j can not be initialized

Explanation

The code will result in a compilation error because the variable "j" in class A is declared as protected, which means it can only be accessed within the same package or by subclasses. In this case, class B is in a different package and does not inherit class A, so it cannot access the variable "j".

Submit
38. 11. class Mud { 12. // insert code here 13. System.out.println("hi"); 14. } 15. } And the following five fragments: public static void main(String...a) { public static void main(String.* a) { public static void main(String... a) { public static void main(String[]... a) { public static void main(String...[] a) { How many of the code fragments, inserted independently at line 12, compile? 0 1 2 3

Explanation

Option 4 is the correct answer because it is the only code fragment that will compile successfully. The ellipsis (...) in the method parameter of Option 4 allows for variable arguments, meaning that the main method can accept any number of String arrays as arguments. This is the correct syntax for the main method in Java. The other options have incorrect syntax for the main method and will not compile.

Submit
39. Public class Q { public static void main(String argv[]) { int anar[] = new int[] { 1, 2, 3 }; System.out.println(anar[1]); } } Compiler Error: anar is referenced before it is initialized 2 1 Compiler Error: size of array must be defined

Explanation

The correct answer is Option 2 because the code initializes an array called "anar" with values 1, 2, and 3. The line "System.out.println(anar[1]);" prints the value at index 1 of the array, which is 2.

Submit
40. Consider the code below & select the correct ouput from the options: public class Test{ public static void main(String[] args) { String num=""; z: for(int x=0;x<3;x++) for(int y=0;y<2;y++){ if(x==1) break; if(x==2 && y==1) break z; num=num+x+y; }System.out.println(num);}} 0 0 0 1 0 0 0 1 2 0 0 0 0 1 2 0 2 1 Compilation error

Explanation

The code uses nested for loops to iterate over the values of x and y. It checks two conditions: if x is equal to 1, it breaks out of the inner loop, and if x is equal to 2 and y is equal to 1, it breaks out of the outer loop labeled "z".

In the given code, the value of num is initialized as an empty string. Inside the nested loops, the value of num is concatenated with the values of x and y.

The output will be "001020" because the code goes through the nested loops three times (x=0,1,2 and y=0,1) and concatenates the values of x and y to the string num.

Submit
41. Consider the following code and choose the correct output: class Test{ public static void main(String args[]){ boolean flag=true; if(flag=false){ System.out.print("TRUE");}else{ System.out.print("FALSE");}}} true false compilation error Compiles

Explanation

The correct output for the given code is "false". This is because in the if statement, the condition `flag=false` is assigning the value `false` to the variable `flag` instead of checking if it is equal to `false`. Therefore, the condition is always false and the code inside the `else` block is executed, which prints "FALSE".

Submit
42. Given: public class Test { public enum Dogs {collie, harrier, shepherd}; public static void main(String [] args) { Dogs myDog = Dogs.shepherd; switch (myDog) { case collie: System.out.print("collie "); case default: System.out.print("retriever "); case harrier: System.out.print("harrier "); } } } What is the result? harrier shepherd retriever Compilation fails.

Explanation

The code will not compile because the "default" case in the switch statement is not a valid case label. In a switch statement, the "default" case should be the last case and it is used as a fallback option when none of the other cases match. Since it is not in the correct position, the code will fail to compile.

Submit
43. Consider the following code and choose the correct option: package aj; private class S{ int roll; S(){roll=1;} } package aj; class T { public static void main(String ar[]){ System.out.print(new S().roll);}} 1.Compilation error 2.Compiles and display 1 3.Compiles but no output 4.Compiles and display 0

Explanation

The code will result in a compilation error because the class S is declared as private, which means it can only be accessed within the same package. However, the class T is in a different package, so it cannot access the private class S.

Submit
44. Next() method of Scanner class will return _________ Integer Long int String

Explanation

The next() method of the Scanner class will return a String.

Submit
45. Given: public class Breaker2 { static String o = ""; public static void main(String[] args) { z: for(int x = 2; x < 7; x++) { if(x==3) continue; if(x==5) break z; o = o + x; } System.out.println(o); } } What is the result? 2 24 234 246

Explanation

The code uses a for loop to iterate from 2 to 6. Inside the loop, there are two conditional statements. The first one checks if the current value of x is equal to 3, and if it is, the continue statement skips the rest of the loop and moves on to the next iteration. The second conditional statement checks if the current value of x is equal to 5, and if it is, the break statement breaks out of the loop labeled "z".

In this case, when x is equal to 5, the break statement is executed, causing the loop to terminate. Before the break statement is executed, the value of x (5) is concatenated to the string "o". Therefore, the final value of "o" is "2" + "4" = "24".

So, the correct answer is Option 2.

Submit
46. Which collection class allows you to access its elements by associating a key with an element's value, and provides synchronization? java.util.SortedMap java.util.TreeMap java.util.TreeSet java.util.Hashtable

Explanation

The correct answer is java.util.Hashtable. Hashtable allows accessing its elements by associating a key with an element's value through key-value pairs. It provides synchronization, meaning that multiple threads can safely access and modify the Hashtable concurrently without causing data corruption.

Submit
47. Which of the following declarations are correct? (Choose TWO) 1.boolean b =TRUE; 2.byte b = 256; 3.String s ="null"; 4.int i = newInteger("56");

Explanation

not-available-via-ai

Submit
48. Import java.util.StringTokenizer; class ST{ public static void main(String[] args){ String input = "Today is$Holiday"; StringTokenizer st = new StringTokenizer(input,"$"); while(st.hasMoreTokens()){ System.out.println(st.nextElement()); }} Today is Holiday Today is Holiday Both none of the listed options

Explanation

The given code uses the StringTokenizer class to tokenize the input string "Today is$Holiday" using the delimiter "$". This means that the string will be split into two tokens: "Today is" and "Holiday". The while loop then iterates through each token using the hasMoreTokens() method and prints each token using the nextElement() method. Therefore, the output will be "Today is" on one line and "Holiday" on the next line.

Submit
49. You wish to store a small amount of data and make it available for rapid access. You do not have a need for the data to be sorted, uniqueness is not an issue and the data will remain fairly static Which data structure might be most suitable for this requirement? 1) TreeSet 2) HashMap 3) LinkedList 4) an array 1 2 3 4

Explanation

An array would be the most suitable data structure for this requirement because it allows for rapid access to data, especially when the amount of data is small. Arrays have constant time complexity for accessing elements by index, making them efficient for this scenario. Additionally, since sorting and uniqueness are not concerns and the data will remain fairly static, an array would provide a simple and straightforward solution.

Submit
50. Abstract class MineBase { abstract void amethod(); static int i; } public class Mine extends MineBase { public static void main(String argv[]){ int[] ar=new int[5]; for(i=0;i < ar.length;i++) System.out.println(ar[i]); } } A Sequence of 5 zero's will be printed like 0 0 0 0 0 A Sequence of 5 one's will be printed like 1 1 1 1 1 IndexOutOfB oundes Error Compilation Error occurs and to avoid them we need to declare Mine class as abstract

Explanation

The code will compile successfully and run without any errors. It will print a sequence of 5 zeros (0 0 0 0 0) because the array "ar" is initialized with default values of zero. The for loop iterates over the array and prints each element.

Submit
51. Given: public class Yikes { public static void go(Long n) {System.out.print("Long ");} public static void go(Short n) {System.out.print("Short ");} public static void go(int n) {System.out.print("int ");} public static void main(String [] args) { short y = 6; long z = 7; go(y); go(z); } } What is the result? int Long Short Long Compilation fails. An exception is thrown at runtime.

Explanation

The correct answer is "int Long" because the go() method is overloaded with three different parameters: Long, Short, and int. When the go() method is called with a short variable, it matches the method with the closest data type, which is int. When the go() method is called with a long variable, it matches the method with the Long parameter. Therefore, the output will be "int Long".

Submit
52. A) No argument constructor is provided to all Java classes by default B) No argument constructor is provided to the class only when no constructor is defined. C) Constructor can have another class object as an argument D) Access specifiers are not applicable to Constructor 1.Only A is TRUE 2.All are TRUE 3.B and C is TRUE 4.All are FALSE

Explanation

Option 3 is the correct answer because both statement B and C are true. Statement B states that a no argument constructor is provided to the class only when no constructor is defined. Statement C states that a constructor can have another class object as an argument.

Submit
53. Given: public static Collection get() { Collection sorted = new LinkedList(); sorted.add("B"); sorted.add("C"); sorted.add("A"); return sorted; } public static void main(String[] args) { for (Object obj: get()) { System.out.print(obj + ", "); } } What is the result? A, B, C, B, C, A, Compilation fails. An exception is thrown at runtime.

Explanation

The code first creates a LinkedList called "sorted" and adds the strings "B", "C", and "A" to it. Then, in the main method, a for-each loop is used to iterate over the elements of the collection returned by the get() method. The elements are printed out with a comma and space after each one. Since the elements were added to the LinkedList in the order "B", "C", "A", they will be printed out in that same order. Therefore, the result will be "B, C, A, ".

Submit
54. Consider the following code and choose the correct output: class Test{ public static void main(String args[]){ TreeMap<Integer, String> hm=new TreeMap<Integer, String>(); hm.put(2,"Two"); hm.put(4,"Four"); hm.put(1,"One"); hm.put(6,"Six"); hm.put(7,"Seven"); SortedMap<Integer, String> sm=hm.subMap(2,7); SortedMap<Integer,String> sm2=sm.tailMap(4); System.out.print(sm2); }} {2=Two, 4=Four, 6=Six, 7=Seven} {4=Four, 6=Six, 7=Seven} {4=Four, 6=Six} {2=Two, 4=Four, 6=Six}

Explanation

The code creates a TreeMap object "hm" and adds key-value pairs to it. Then, it creates a SortedMap "sm" by using the subMap() method on "hm" with the range (2,7). Finally, it creates another SortedMap "sm2" by using the tailMap() method on "sm" with the starting key 4. The print statement outputs the elements in "sm2", which are {4=Four, 6=Six}. Therefore, the correct output is Option 3.

Submit
55. Given: import java.util.Arrays; import java.util.HashSet; import java.util.Set; public class MainClass { public static void main(String[] a) { String elements[] = { "A", "B", "C", "D", "E" }; Set set = new HashSet(Arrays.asList(elements)); elements = new String[] { "A", "B", "C", "D" }; Set set2 = new HashSet(Arrays.asList(elements)); System.out.println(set.equals(set2)); } } What is the result of given code? true false Compile time error Runtime Exception

Explanation

The result of the given code is false. This is because the first set, 'set', contains all the elements "A", "B", "C", "D", and "E", while the second set, 'set2', only contains the elements "A", "B", "C", and "D". Therefore, the two sets are not equal, resulting in a false value when comparing them using the equals() method.

Submit
56. Object get(Object key) - What does this method return if the key is not found in the Map? 0 -1 null none of the listed options

Explanation

The method get(Object key) returns null if the key is not found in the Map.

Submit
57. What will be printed out if you attempt to compile and run the following code ? public class AA { public static void main(String[] args) { int i = 9; switch (i) { default: System.out.println("default"); case 0: System.out.println("zero"); break; case 1: System.out.println("one"); case 2: System.out.println("two"); } } } Compilation Error default default zero default zero one two

Explanation

The code will compile and run without any errors. When the value of i is 9, it does not match any of the cases in the switch statement. Therefore, the code will execute the default block and print "default" to the console. After that, it will fall through to the case 0 block and print "zero" to the console. The break statement after case 0 will then exit the switch statement, so the code will not execute the case 1 and case 2 blocks. Therefore, the output will be "default zero".

Submit
58. Select the variable which are in java.lang.annotation.RetentionPolicy class. (Choose THREE) SOURCE CLASS RUNTIME CONSTRUCTOR

Explanation

The correct answer is Option1, Option2, Option3. These options are the correct variables in the java.lang.annotation.RetentionPolicy class. This class is used to specify the retention policy for annotations in Java. The variables in this class are SOURCE, CLASS, and RUNTIME. These variables represent the different retention policies that can be applied to annotations. The SOURCE policy means that the annotation is only available in the source code and is not included in the compiled code. The CLASS policy means that the annotation is included in the compiled code but not available at runtime. The RUNTIME policy means that the annotation is available at runtime.

Submit
59. What is the result of attempting to compile and run the following code? import java.util.Vector; import java.util.LinkedList; public class Test1{ public static void main(String[] args) { Integer int1 = new Integer(10); Vector vec1 = new Vector(); LinkedList list = new LinkedList(); vec1.add(int1); list.add(int1); if(vec1.equals(list)) System.out.println("equal"); else System.out.println("not equal"); } } 1. The code will fail to compile. 2. Runtime error due to incompatible object comparison 3. Will run and print "equal". 4. Will run and print "not equal". 1 2 3 4 A.1 B.2 C.3 D.4

Explanation

The code will run and print "equal" because the Vector and LinkedList both contain the same Integer object, int1. The equals() method in the Vector class compares the elements of the Vector with the elements of the LinkedList, and since they both contain the same Integer object, the comparison returns true. Therefore, the code will execute the if statement and print "equal".

Submit
60. What is the range of the random number r generated by the code below? int r = (int)(Math.floor(Math.random() * 8)) + 2; 2 <= r <= 9 3 <= r <= 10 2<= r <= 10 3 <= r <= 9

Explanation

The code generates a random number between 0 and 7 using Math.random() and then multiplies it by 8. Math.floor() is used to round down the decimal value to the nearest integer. The resulting value is then added to 2. Therefore, the range of the random number r is 2

Submit
61. Public class c123 { private c123() { System.out.println("Hellow"); } public static void main(String args[]) { c123 o1 = new c123(); c213 o2 = new c213(); } } class c213 { private c213() { System.out.println("Hello123"); } } What is the output? Hellow It is not possible to declare a constructor as private Compilation Error Runs without any output

Explanation

The output of the code will be "Runs without any output". This is because the main method creates an object of class c123, which in turn creates an object of class c213. However, the constructor of class c213 is declared as private, which means it cannot be accessed outside of the class. Therefore, the object of class c213 cannot be created and the code does not produce any output.

Submit
62. A) Iterator does not allow to insert elements during traversal B) Iterator allows bidirectional navigation. C) ListIterator allows insertion of elements during traversal D) ListIterator does not support bidirectional navigation A and B is TRUE A and D is TRUE A and C is TRUE B and D is TRUE

Explanation

The correct answer is option 3, "A and C is TRUE." This is because the statement in option A is true - an iterator does not allow for the insertion of elements during traversal. Additionally, the statement in option C is also true - a ListIterator does allow for the insertion of elements during traversal.

Submit
63. Static int binarySearch(List list, Object key) is a method of __________ Vector class ArrayList class Collection interface Collections class

Explanation

The method binarySearch(List list, Object key) is a method of the Collections class. This class contains various utility methods for manipulating and searching collections. The binarySearch() method specifically performs a binary search on the specified list to find the index of the specified key object.

Submit
64. Given the following code what will be output? public class Pass{ static int j=20; public static void main(String argv[]){ int i=10; Pass p = new Pass(); p.amethod(i); System.out.println(i); System.out.println(j); } public void amethod(int x){ x=x*2; j=j*2; } } 1.Error: amethod parameter does not match variable 2.20 and 40 3.10 and 40 4.10, and 20

Explanation

The code creates an instance of the Pass class and calls the amethod() method on it, passing in the value of i (which is 10). Inside the amethod() method, the value of x is multiplied by 2, but this does not affect the value of i outside of the method. However, the value of j (which is a static variable) is also multiplied by 2 inside the amethod() method, so when it is printed out in the main method, it is 40. Therefore, the output will be 10 and 40.

Submit
65. Choose the meta annotations. (Choose THREE) Target Retention Depricated Documented

Explanation

The correct answer is Option1, Option2, and Option4. These are the meta annotations that can be used in Java. "Target" is used to specify the element types that the annotation can be applied to. "Retention" is used to specify how long the annotation should be retained. "Documented" is used to indicate that the annotation should be included in the generated documentation. "Deprecated" is not a meta annotation, but rather an annotation used to indicate that a program element is no longer recommended for use.

Submit
66. Cosider the following code and choose the correct option: class Test{ public static void main(String args[]){ System.out.println(Integer.parseInt("21474836 48", 10)); }} Compilation error 2.15E+09 NumberFormatException at run time Compiles butno output

Explanation

The code snippet is attempting to parse the string "2147483648" as an integer using the Integer.parseInt() method. However, this string exceeds the maximum value that an integer can hold (2,147,483,647), resulting in a NumberFormatException at runtime. Therefore, the correct option is option 3, which states "NumberFormatException at run time".

Submit
67. A)Property files help to decrease coupling B) DateFormat class allows you to format dates and times with customized styles. C) Calendar class allows to perform date calculation and conversion of dates and times between timezones. D) Vector class is not synchronized A and B is TRUE A and D is TRUE A and C is TRUE B and D is TRUE

Explanation

Option 3 is the correct answer because both statement A and statement C are true. Property files help to decrease coupling by providing a way to externalize configuration data, reducing the dependency between components. The Calendar class allows for date calculation and conversion between timezones, providing a convenient way to work with dates and times.

Submit
68. Class One{ int var1; One (int x){ var1 = x; }} class Derived extends One{ int var2; Derived(){ super(10); var2=10; } void display(){ System.out.println("var1="+var1+" , var2="+var2); }} class Main{ public static void main(String[] args){ Derived obj = new Derived(); obj.display(); }} consider the code above & select the proper output from the options. var1=10 , var2=10 0,0 compile error runtime error

Explanation

The code creates three classes: One, Derived, and Main. The Derived class extends the One class and adds a new variable var2. The Derived class also has a display() method that prints the values of var1 and var2. In the main method of the Main class, an object of the Derived class is created and the display() method is called. Since the constructor of the Derived class calls the super constructor with an argument of 10, var1 is initialized to 10. The var2 variable is also initialized to 10. Therefore, the output will be "var1=10, var2=10".

Submit
69. What will be the result of compiling the following program? public class MyClass { long var; public void MyClass(long param) { var = param; } // (Line no 1) public static void main(String[] args) { MyClass a, b; a = new MyClass(); // (Line no 2)  } } 1.A compilation error will occur at (Line no 1), since constructors cannot specify a return value 2.A compilation error will occur at (2), since the class does not have a default constructor 3.A compilation error will occur at (Line no 2), since the class does not have a constructor that takes one argument of type int. 4.The program will compile without errors.

Explanation

The program will compile without errors because it does not have any syntax errors. There is no compilation error at (Line no 1) because the constructor in the class is correctly defined. There is also no compilation error at (Line no 2) because the class does not need a default constructor in order to create an object.

Submit
70. Given: import java.util.*; public class LetterASort{ public static void main(String[] args) { ArrayList<String> strings = new ArrayList<String>(); strings.add("aAaA"); strings.add("AaA"); strings.add("aAa"); strings.add("AAaa"); Collections.sort(strings); for (String s : strings) { System.out.print(s + " "); } } } What is the result? Compilation fails. aAaA aAa AAaa AaA AAaa AaA aAa aAaA AaA AAaa aAaA aAa

Explanation

The code snippet creates an ArrayList of strings and adds four strings to it. It then uses the Collections.sort() method to sort the strings in lexicographical order. The for-each loop is used to print each string in the sorted order. The expected output is "AaA AAaa aAa aAaA". Therefore, the correct answer is Option 3.

Submit
71. A) It is a good practice to store heterogenous data in a TreeSet. B) HashSet has default initial capacity (16) and loadfactor(0.75) C)HashSet does not maintain order of Insertion D)TreeSet maintains order of Inserstion A and B is TRUE A and D is TRUE A and C is TRUE B and C is TRUE

Explanation

Option 4 (B and C is TRUE) is the correct answer because HashSet does not maintain the order of insertion (C) and it has a default initial capacity of 16 and a load factor of 0.75 (B). Option 1 (A and B is TRUE) is incorrect because storing heterogeneous data in a TreeSet (A) is not a good practice. Option 2 (A and D is TRUE) is incorrect because storing heterogeneous data in a TreeSet (A) is not a good practice. Option 3 (A and C is TRUE) is incorrect because storing heterogeneous data in a TreeSet (A) is not a good practice.

Submit
72. Consider the following code and choose the correct option: class A{ A(){System.out.print("From A");}} class B extends A{ B(int z){z=2;} public static void main(String args[]){ new B(3);}} 1.Compilation error 2.Comiples and prints From A 3.Compiles but throws runtime exception 4.Compiles and display 3

Explanation

The code will compile and print "From A" because the constructor of class A is called when an object of class B is created. The constructor of class B does not have any print statements, so it does not print anything.

Submit
73. What will be the result when you try to compile and run the following code? private class Base{ Base(){ int i = 100; System.out.println(i); } } public class Pri extends Base{ static int i = 200; public static void main(String argv[]){ Pri p = new Pri(); System.out.println(i); } } 1. 200 2.100 followed by 200 3.Compile time error 4.100

Explanation

The code will result in a compile time error because the class "Base" is declared as private. Private classes cannot be subclassed, so the class "Pri" cannot extend "Base".

Submit
74. Consider the following code and choose the correct option: class A{ private void display(){ System.out.print("Hi");} public static void main(String ar[]){ display();}} Compiles but doesn't display anything Compiles and throws run time exception Compilation fails Compiles and displays Hi

Explanation

not-available-via-ai

Submit
75. Which collection class allows you to grow or shrink its size and provides indexed access to its elements, but its methods are not synchronized? java.util.HashSet java.util.LinkedHashSet java.util.List java.util.ArrayList

Explanation

ArrayList is the correct answer because it allows you to dynamically grow or shrink its size using methods like add() and remove(). It also provides indexed access to its elements, meaning you can access elements by their position in the list using methods like get(). ArrayList is not synchronized, meaning it is not thread-safe and multiple threads can access and modify it concurrently without any restrictions.

Submit
76. Which of the following sentences is true? A) Access to data member depends on the scope of the class and the scope of data members B) Access to data member depends only on the scope of the data members C) Access to data member depends on the scope of the method from where it is accessed Only A and C is TRUE All are TRUE All are FALSE Only A is TRUE

Explanation

The correct answer is Option 4. This means that only statement A is true. Statement A states that access to a data member depends on the scope of the class and the scope of the data members. This is true because the scope of a data member is determined by the class it belongs to, and the access to the data member is limited to within the scope of the class. The other statements (B and C) are false because they do not accurately describe the access to a data member.

Submit
77. Consider the following code and select the correct output: import java.util.ArrayList; import java.util.LinkedList; import java.util.List; public class Lists { public static void main(String[] args) { List<String> list=new ArrayList<String>(); list.add("1"); list.add("2"); list.add(1, "3"); List<String> list2=new LinkedList<String>(list); list.addAll(list2); list2 =list.subList(2,5); list2.clear(); System.out.println(list); } } [1,3,2] [1,3,3,2] [1,3,2,1,3,2] [3,1,2]

Explanation

The correct output is [1, 3, 2] because the code first creates an ArrayList called "list" and adds the elements "1" and "2" to it. Then, it inserts the element "3" at index 1 in the list, resulting in [1, 3, 2]. Next, a LinkedList called "list2" is created and initialized with the elements from "list". The elements from "list2" are then added to "list", resulting in [1, 3, 2, 1, 3, 2]. Finally, a sublist containing elements from index 2 to 4 of "list" is assigned to "list2" and cleared, resulting in [1, 3, 2].

Submit
78. Given: public class Test { public enum Dogs {collie, harrier}; public static void main(String [] args) { Dogs myDog = Dogs.collie; switch (myDog) { case collie: System.out.print("collie "); case harrier: System.out.print("harrier "); } } } What is the result? collie harrier Compilation fails. collie harrier

Explanation

The code will print "collie harrier". This is because the switch statement is using the variable "myDog" which is set to "Dogs.collie". When the switch statement is evaluated, it matches the "collie" case and prints "collie". However, there is no break statement after the "collie" case, so it falls through to the next case, "harrier", and prints "harrier" as well. Therefore, the output is "collie harrier".

Submit
79. Which interface does java.util.Hashtable implement? Java.util.Map Java.util.List Java.util.Table Java.util.Collection

Explanation

The correct answer is Java.util.Map. This is because Hashtable is a class in the Java.util package that implements the Map interface. The Map interface provides a way to store key-value pairs, and Hashtable is one of the implementations of this interface.

Submit
80. Select the Uses of annotations. (Choose THREE) Information For the Compiler Information for the JVM Compile time and deploytime processing Runtime processing

Explanation

Annotations are used for providing additional information to the compiler, JVM, and for processing at compile time, deploy time, and runtime. Option 1 is correct because annotations can be used to provide information to the compiler. Option 3 is correct because annotations can be processed during compile time and deploy time. Option 4 is correct because annotations can be processed at runtime.

Submit
81. What will be the output of following code? class Test{ public static void main(String args[]){ TreeSet<Integer> ts=new TreeSet<Integer>(); ts.add(2); ts.add(3); ts.add(7); ts.add(5); SortedSet<Integer> ss=ts.subSet(1,7); ss.add(4); ss.add(6); System.out.print(ss);}} [2,3,7,5] [2,3,7,5,4,6] [2,3,4,5,6,7] [2,3,4,5,6]

Explanation

The code creates a TreeSet object named "ts" and adds integers 2, 3, 7, and 5 to it. Then, it creates a SortedSet object named "ss" using the subSet() method of ts, with the range from 1 to 7. The subSet() method returns a view of the portion of the original set that falls within the specified range.

Next, the code adds integers 4 and 6 to the SortedSet "ss". Since "ss" is a view of "ts", adding elements to "ss" also adds them to "ts".

Finally, the code prints the elements of "ss", which are 2, 3, 4, 5, and 6. Therefore, the correct answer is [2, 3, 4, 5, 6].

Submit
82. Consider the following code and choose the correct option: class Data{ Integer data; Data(Integer d){data=d;} public boolean equals(Object o){return true;} public int hasCode(){return 1;}} class Test{ public static void main(String ar[]){ Set<Data> s=new HashSet<Data>(); s.add(new Data(4)); s.add(new Data(2)); s.add(new Data(4)); s.add(new Data(1)); s.add(new Data(2)); System.out.print(s.size());}} 3 5 compilation error Compiles but error at run time

Explanation

The code creates a HashSet and adds five Data objects to it. The Data class overrides the equals() method to always return true, which means that all Data objects are considered equal. However, the Data class does not override the hashCode() method, so the default implementation is used. This means that each Data object will have a different hash code, and they will be stored as separate elements in the HashSet. Therefore, the size of the HashSet will be 5.

Submit
83. Static void sort(List list) method is part of ________ Collection interface Collections class Vector class ArrayList class

Explanation

The static void sort(List list) method is part of the Collections class. This method is used to sort the elements in the specified list in ascending order.

Submit
84. Which modifier indicates that the variable might be modified asynchronously, so that all threads will get the correct value of the variable. synchronized volatile transient default

Explanation

The correct answer is "volatile". The volatile modifier indicates that the variable might be modified asynchronously, so that all threads will get the correct value of the variable.

Submit
85. Consider the following code and choose the best option: class Super{ int x; Super(){x=2;}} class Sub extends Super { void displayX(){ System.out.print(x);} public static void main(String args[]){ new Sub().displayX();}} Compilation error Compiles and runs without any output Compiles and display 2 Compiles and display 0

Explanation

The code compiles and displays 0. The class Sub inherits from the class Super and does not have its own constructor, so it uses the default constructor from the superclass. The default constructor initializes the variable x to 2. However, when the displayX() method is called, it prints the value of x, which is 0 because it has not been assigned a new value in the Sub class.

Submit
86. Consider the following code and choose the correct option: class A{ int z; A(int x){z=x;} } class B extends A{ public static void main(String arg){ new B();}} 1.Compilation error 2.Compiles but throws run time exception 3.Compiles and displays nothing 4.None of the listed options

Explanation

The code will result in a compilation error because class B is extending class A, but class A does not have a default constructor. Therefore, when creating an object of class B in the main method, it will try to call the default constructor of class A, which does not exist.

Submit
87. TreeSet<String> s = new TreeSet<String>(); TreeSet<String> subs = new TreeSet<String>(); s.add("a"); s.add("b"); s.add("c"); s.add("d"); s.add("e"); subs = (TreeSet)s.subSet("b", true, "d", true); s.add("g"); s.pollFirst(); s.pollFirst(); s.add("c2"); System.out.println(s.size() +" "+ subs.size()); The size of s is 4 The size of s is 5 The size of subs is 3 The size of s is 7

Explanation

The size of s is initially 5. After removing the first two elements using the pollFirst() method, adding "c2" to s, and printing the size of s and subs, the size of s becomes 4. However, the size of subs remains 3 because it is a subset of s and the elements "b" and "d" are inclusive in the subset. Therefore, the correct answers are Option 2 and Option 3.

Submit
88. Public class MyAr { public static void main(String argv[]) { MyAr m = new MyAr(); m.amethod(); } public void amethod() { final int i1; System.out.println(i1); } } What is the Output of the Program? 0 Unresolved compilation problem: The local variable i1 may not have been initialized Compilation and output of null None of the given options

Explanation

The output of the program is a compilation problem: The local variable i1 may not have been initialized. This is because the variable i1 is declared as final but not assigned a value before it is used in the System.out.println statement.

Submit
89. Which statements, when inserted at (1), will not result in compile-time errors? public class ThisUsage { int planets; static int suns; public void gaze() { int i; // (1) INSERT STATEMENT HERE } } i =this.planets; i = this.suns; this = newThisUsage(); this.suns =planets;

Explanation

Option 1, Option 2, and Option 4 will not result in compile-time errors when inserted at (1).

Option 1 assigns the value of the instance variable "planets" to the local variable "i" using the "this" keyword.

Option 2 assigns the value of the static variable "suns" to the local variable "i" using the class name "thisUsage".

Option 4 assigns the value of the instance variable "planets" to the static variable "suns" using the "this" keyword.

Submit
90. Class Test{ static void method(){ this.display(); } static display(){ System.out.println(("hello"); } public static void main(String[] args){ new Test().method(); } } consider the code above & select the proper output from the options. 1.hello 2.Runtime Error 3.compiles but no output 4.does not compile

Explanation

The code will not compile because there are syntax errors in the code. The method "display" is missing a return type and the closing parenthesis in the System.out.println statement is missing. Therefore, the correct answer is option 4: does not compile.

Submit
91. Given: public static Iterator reverse(List list) { Collections.reverse(list); return list.iterator(); } public static void main(String[] args) { List list = new ArrayList(); list.add("1"); list.add("2"); list.add("3"); for (Object obj: reverse(list)) System.out.print(obj + ", "); } What is the result? 3, 2, 1 1, 2, 3 Compilation fails. The code runs with no output.

Explanation

The code will compile successfully and run without any errors. The reverse() method uses the Collections.reverse() method to reverse the order of the elements in the list. The reverse() method then returns an iterator over the reversed list. In the main() method, a for-each loop is used to iterate over the elements returned by the reverse() method. The elements in the reversed list are printed in reverse order, resulting in the output "3, 2, 1".

Submit
92. Consider the following code and choose the correct option: class Test{ public static void main(String args[]){ Integer arr[]={3,4,3,2}; Set<Integer> s=new TreeSet<Integer>(Arrays.asList(arr)); s.add(1); for(Integer ele :s){ System.out.println(ele); } }} Compilation error prints 3,4,2,1  prints 1,2,3,4 Compiles but exception at runtime

Explanation

The code compiles without any errors and runs successfully. It creates a TreeSet named "s" and adds the elements from the array "arr" to it. Then, it adds the element 1 to the TreeSet. The TreeSet automatically sorts the elements in ascending order. Finally, it iterates over the TreeSet and prints each element, resulting in the output "1, 2, 3, 4".

Submit
93. Consider the following code and choose the correct option: class Test{ private void display(){ System.out.println("Display()");} private static void show() { display(); System.out.println("show()");} public static void main(String arg[]){ show();}} Compiles and prints show() Compiles and prints Display() show() Compiles but throws runtime exception Compilation error

Explanation

The code will result in a compilation error because the method `display()` is private and can only be accessed within the same class. Since the `show()` method is trying to call `display()`, which is not accessible, a compilation error will occur.

Submit
94. Which statement is true about the following program? import java.util.ArrayList; import java.util.Collections; import java.util.List; public class WhatISThis { public static void main(String[] na){ List<StringBuilder> list=new ArrayList<StringBuilder>(); list.add(new StringBuilder("B")); list.add(new StringBuilder("A")); list.add(new StringBuilder("C")); Collections.sort(list,Collections.reverseOrder() ); System.out.println(list.subList(1,2)); } } The program will compile and print the following output: [B] The program will compile and print the following output: [B,A] The program will compile and throw a runtime exception The program will not compile

Explanation

The program will compile and throw a runtime exception. This is because the program is trying to sort a list of StringBuilder objects using the Collections.reverseOrder() method, which is designed to sort objects in reverse order. However, StringBuilder does not implement the Comparable interface, which is required for sorting. Therefore, a ClassCastException will be thrown at runtime.

Submit
95. Public class MyAr { public static void main(String argv[]) { MyAr m = new MyAr(); m.amethod(); } public void amethod() { static int i1; System.out.println(i1); } } What is the Output of the Program? 0 Compile time error because i has not been initialized Compilation and output of null It is not possible to declare a static variable in side of non static method or instance method. Because Static variables are class level dependencies.

Explanation

The program will not compile because it is not possible to declare a static variable inside a non-static method. Static variables are class-level dependencies and should be declared outside of any method. In this case, the variable "i1" is declared as static inside the "amethod()" method, which is not allowed. Hence, the correct answer is option 4.

Submit
96. Public class MyAr { static int i1; public static void main(String argv[]) { MyAr m = new MyAr(); m.amethod(); } public void amethod() { System.out.println(i1); } } What is the output of the program? 0 Compilation Error Garbage Value It is not possible to access a static variable in side of non static method

Explanation

The output of the program is 0 because the variable i1 is a static variable and its default value is 0. The main method creates an instance of the MyAr class and calls the amethod. The amethod then prints the value of i1, which is 0.

Submit
97. Public class c1 { private c1() { System.out.println("Hello"); } public static void main(String args[]) { c1 o1=new c1(); } } What is the output? Hello It is not possible to declare a constructor private Compilation Error Can't create object because constructor is private

Explanation

The output of the given code will be "Hello". This is because the class "c1" has a private constructor, which means that it cannot be accessed from outside the class. However, within the class itself, the private constructor can be invoked. In the main method, an object of class "c1" is created using the constructor, and when this object is instantiated, the constructor is called and it prints "Hello" to the console.

Submit
98. Consider the following code and choose the correct option: class X { int x; X(int x){x=2;}} class Y extends X{ Y(){} void displayX(){ System.out.print(x);} public static void main(String args[]){ new Y().displayX();}} Compiles and display 2 Compiles and runs without any output Compiles and display 0 Compilation error

Explanation

The code will result in a compilation error because the constructor of class Y does not call the constructor of its superclass X. Therefore, the instance variable x in class X is never initialized, and when the displayX() method tries to print the value of x, it will result in an error.

Submit
99. Consider the following code and choose the correct option: class A{ int a; A(int a){a=4;}} class B extends A{ B(){super(3);} void displayA(){ System.out.print(a);} public static void main(String args[]){ new B().displayA();}} compiles and display 0 compilation error Compiles and display 4 Compiles and display 3

Explanation

The code compiles and displays 0 because the constructor of class A is not properly initializing the instance variable "a". In the constructor of class A, the parameter "a" is assigned to itself instead of the instance variable. Therefore, the default value of "a" (which is 0) is printed when the displayA() method is called.

Submit
100. Consider the following code and choose the correct option: public static void before() { Set set = new TreeSet(); set.add("2"); set.add(3); set.add("1"); Iterator it = set.iterator(); while (it.hasNext()) System.out.print(it.next() + " "); } The before() method will print 1 2 The before() method will print 1 2 3 The before() method will throw an exception at runtime The before() method will not compile

Explanation

The before() method will throw an exception at runtime because a TreeSet cannot contain elements of different types. In this case, the set contains both String and Integer objects, which are not compatible. Therefore, when trying to iterate over the set, a ClassCastException will be thrown.

Submit
View My Results

Quiz Review Timeline (Updated): Sep 18, 2023 +

Our quizzes are rigorously reviewed, monitored and continuously updated by our expert board to maintain accuracy, relevance, and timeliness.

  • Current Version
  • Sep 18, 2023
    Quiz Edited by
    ProProfs Editorial Team
  • Jan 08, 2019
    Quiz Created by
    Rahul Kumar
Cancel
  • All
    All (100)
  • Unanswered
    Unanswered ()
  • Answered
    Answered ()
Class A, B and C are in multilevel inheritance ...
What will happen when you attempt to ...
If no retention policy is specified for an ...
Package QB; ...
Class Test{ ...
Class MyClass1 ...
Consider the following code and choose the ...
Which modifier is used to control access to ...
A) A call to instance method can not be made ...
Public class MyClass { ...
What will be the result when you attempt to ...
Which of the following will print -4.0 ...
Custom annotations can be created using ...
What will happen if a main() method of a ...
Class Order{ ...
Consider the following code and choose the ...
Consider the following code and choose the ...
Class Sample ...
Class One{ ...
Consider the following code and choose the ...
Consider the following code was executed on ...
Class Order{ ...
A constructor may return value including class type
All annotation types should maually extend the Annotation interface.
Nt indexOf(Object o) - What does this method ...
Given: ...
Consider the following code and choose the ...
Consider the code below & select the correct ...
Given: ...
Here is the general syntax for method ...
Class A { ...
Package QB; ...
Class Order{ ...
Given: ...
Suppose class B is sub class of class A: ...
Inorder to remove one element from the given ...
Consider the following code and choose the ...
11. class Mud { ...
Public class Q { ...
Consider the code below & select the correct ...
Consider the following code and choose the ...
Given: ...
Consider the following code and choose the ...
Next() method of Scanner class will return ...
Given: ...
Which collection class allows you to access ...
Which of the following declarations are ...
Import java.util.StringTokenizer; ...
You wish to store a small amount of data and ...
Abstract class MineBase { ...
Given: ...
A) No argument constructor is provided to all ...
Given: ...
Consider the following code and choose the ...
Given: ...
Object get(Object key) - What does this ...
What will be printed out if you attempt to ...
Select the variable which are in ...
What is the result of attempting to compile ...
What is the range of the random number r ...
Public class c123 { ...
A) Iterator does not allow to insert elements ...
Static int binarySearch(List list, Object key) is ...
Given the following code what will be output? ...
Choose the meta annotations. (Choose THREE) ...
Cosider the following code and choose the ...
A)Property files help to decrease coupling ...
Class One{ ...
What will be the result of compiling the ...
Given: ...
A) It is a good practice to store heterogenous ...
Consider the following code and choose the ...
What will be the result when you try to ...
Consider the following code and choose the ...
Which collection class allows you to grow or ...
Which of the following sentences is true? ...
Consider the following code and select the ...
Given: ...
Which interface does java.util.Hashtable ...
Select the Uses of annotations. (Choose ...
What will be the output of following code? ...
Consider the following code and choose the ...
Static void sort(List list) method is part of ...
Which modifier indicates that the variable ...
Consider the following code and choose the ...
Consider the following code and choose the ...
TreeSet<String> s = new TreeSet<String>(); ...
Public class MyAr { ...
Which statements, when inserted at (1), will ...
Class Test{ ...
Given: ...
Consider the following code and choose the ...
Consider the following code and choose the ...
Which statement is true about the following ...
Public class MyAr { ...
Public class MyAr { ...
Public class c1 { ...
Consider the following code and choose the ...
Consider the following code and choose the ...
Consider the following code and choose the ...
Alert!

Advertisement