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,028
| Attempts: 5,438
SettingsSettings
Please wait...
  • 1/100 Questions

    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

    • Option 1
    • Option 2
    • Option 3
    • Option 4
Please wait...
About This 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.

Java Quiz-1 - Quiz

Quiz Preview

  • 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

    • Option 1

    • Option 2

    • Option 3

    • Option 4

    Correct Answer
    A. Option 1
    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.

    Rate this question:

  • 3. 

    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();

    • Option 1

    • Option 2

    • Option 3

    • Option 4

    Correct Answer
    A. Option 2
    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.

    Rate this question:

  • 4. 

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

    • Option 1

    • Option 2

    • Option 3

    • Option 4

    Correct Answer
    A. Option 2
    Explanation
    If no retention policy is specified for an annotation, then the default policy of "class" is used.

    Rate this question:

  • 5. 

    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

    • Option 1

    • Option 2

    • Option 3

    • Option 4

    Correct Answer
    A. Option 3
    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.

    Rate this question:

  • 6. 

    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]

    • Option 1

    • Option 2

    • Option 3

    • Option 4

    Correct Answer
    A. Option 3
    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.

    Rate this question:

  • 7. 

    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

    • Option 1

    • Option 2

    • Option 3

    • Option 4

    Correct Answer
    A. Option 3
    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.

    Rate this question:

  • 8. 

    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

    • Option 1

    • Option 2

    • Option 3

    • Option 4

    Correct Answer
    A. Option 2
    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.

    Rate this question:

  • 9. 

    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

    • Option 1

    • Option 2

    • Option 3

    • Option 4

    Correct Answer
    A. Option 1
    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".

    Rate this question:

  • 10. 

    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));

    • Option 1

    • Option 2

    • Option 3

    • Option 4

    Correct Answer
    A. Option 1
    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.

    Rate this question:

  • 11. 

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

    • Option 1

    • Option 2

    • Option 3

    • Option 4

    Correct Answer
    A. Option 4
    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.

    Rate this question:

  • 12. 

    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

    • Option 1

    • Option 2

    • Option 3

    • Option 4

    Correct Answer
    A. Option 1
    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.

    Rate this question:

  • 13. 

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

    • Option 1

    • Option 2

    • Option 3

    • Option 4

    Correct Answer
    A. Option 1
    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.

    Rate this question:

  • 14. 

    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

    • Option 1

    • Option 2

    • Option 3

    • Option 4

    Correct Answer
    A. Option 4
    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

    Rate this question:

  • 15. 

    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

    • Option 1

    • Option 2

    • Option 3

    • Option 4

    Correct Answer
    A. Option 1
    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.

    Rate this question:

  • 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

    • Option 1

    • Option 2

    • Option 3

    • Option 4

    Correct Answer
    A. Option 2
    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.

    Rate this question:

  • 17. 

    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

    • Option 1

    • Option 2

    • Option 3

    • Option 4

    Correct Answer
    A. Option 3
    Explanation
    The code will not compile because the class Derived does not have a constructor that matches the constructor in the superclass One.

    Rate this question:

  • 18. 

    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

    • Option 1

    • Option 2

    • Option 3

    • Option 4

    Correct Answer
    A. Option 2
    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.

    Rate this question:

  • 19. 

    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

    • Option 1

    • Option 2

    • Option 3

    • Option 4

    Correct Answer
    A. Option 1
    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.

    Rate this question:

  • 20. 

    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

    • Option 1

    • Option 2

    • Option 3

    • Option 4

    Correct Answer
    A. Option 2
    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".

    Rate this question:

  • 21. 

    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.

    • Option 1

    • Option 2

    • Option 3

    • Option 4

    Correct Answer
    A. Option 3
    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.

    Rate this question:

  • 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

    • Option 1

    • Option 2

    • Option 3

    • Option 4

    Correct Answer
    A. Option 1
    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".

    Rate this question:

  • 23. 

    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

    • Option 1

    • Option 2

    • Option 3

    • Option 4

    Correct Answer
    A. Option 2
    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.

    Rate this question:

  • 24. 

    A constructor may return value including class type

    • True

    • False

    Correct Answer
    A. False
    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.

    Rate this question:

  • 25. 

    All annotation types should maually extend the Annotation interface.

    • True

    • False

    Correct Answer
    A. False
    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.

    Rate this question:

  • 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

    • Option 1

    • Option 2

    • Option 3

    • Option 4

    Correct Answer
    A. Option 1
    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.

    Rate this question:

  • 27. 

    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

    • Option 1

    • Option 2

    • Option 3

    • Option 4

    Correct Answer
    A. Option 2
    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.

    Rate this question:

  • 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

    • Option 1

    • Option 2

    • Option 3

    • Option 4

    Correct Answer
    A. Option 3
    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.

    Rate this question:

  • 29. 

    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

    • Option 1

    • Option 2

    • Option 3

    • Option 4

    Correct Answer
    A. Option 1
    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".

    Rate this question:

  • 30. 

    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

    • Option 1

    • Option 2

    • Option 3

    • Option 4

    Correct Answer
    A. Option 3
    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.

    Rate this question:

  • 31. 

    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

    • Option 1

    • Option 2

    • Option 3

    • Option 4

    Correct Answer
    A. Option 3
    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".

    Rate this question:

  • 32. 

    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

    • Option 1

    • Option 2

    • Option 3

    • Option 4

    Correct Answer
    A. Option 1
    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.

    Rate this question:

  • 33. 

    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()

    • Option 1

    • Option 2

    • Option 3

    • Option 4

    Correct Answer
    A. Option 1
    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.

    Rate this question:

  • 34. 

    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

    • Option 1

    • Option 2

    • Option 3

    • Option 4

    Correct Answer
    A. Option 3
    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.

    Rate this question:

  • 35. 

    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

    • Option 1

    • Option 2

    • Option 3

    • Option 4

    Correct Answer
    A. Option 4
    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.

    Rate this question:

  • 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"));

    • Option 1

    • Option 2

    • Option 3

    • Option 4

    Correct Answer
    A. Option 3
    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.

    Rate this question:

  • 37. 

    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

    • Option 1

    • Option 2

    • Option 3

    • Option 4

    Correct Answer
    A. Option 3
    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".

    Rate this question:

  • 38. 

    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

    • Option 1

    • Option 2

    • Option 3

    • Option 4

    Correct Answer
    A. Option 1
    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.

    Rate this question:

  • 39. 

    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

    • Option 1

    • Option 2

    • Option 3

    • Option 4

    Correct Answer
    A. Option 4
    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.

    Rate this question:

  • 40. 

    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

    • Option 1

    • Option 2

    • Option 3

    • Option 4

    Correct Answer
    A. Option 2
    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.

    Rate this question:

  • 41. 

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

    • Option 1

    • Option 2

    • Option 3

    • Option 4

    Correct Answer
    A. Option 4
    Explanation
    The next() method of the Scanner class will return a String.

    Rate this question:

  • 42. 

    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

    • Option 1

    • Option 2

    • Option 3

    • Option 4

    Correct Answer
    A. Option 4
    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.

    Rate this question:

  • 43. 

    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

    • Option 1

    • Option 2

    • Option 3

    • Option 4

    Correct Answer
    A. Option 2
    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.

    Rate this question:

  • 44. 

    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

    • Option 1

    • Option 2

    • Option 3

    • Option 4

    Correct Answer
    A. Option 2
    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".

    Rate this question:

  • 45. 

    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.

    • Option 1

    • Option 2

    • Option 3

    • Option 4

    Correct Answer
    A. Option 4
    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.

    Rate this question:

  • 46. 

    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

    • Option 1

    • Option 2

    • Option 3

    • Option 4

    Correct Answer
    A. Option 2
    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.

    Rate this question:

  • 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”);

    • Option1

    • Option2

    • Option3

    • Option4

    Correct Answer(s)
    A. Option3
    A. Option4
  • 48. 

    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

    • Option 1

    • Option 2

    • Option 3

    • Option 4

    Correct Answer
    A. Option 3
    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.

    Rate this question:

  • 49. 

    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.

    • Option 1

    • Option 2

    • Option 3

    • Option 4

    Correct Answer
    A. Option 1
    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".

    Rate this question:

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
Back to Top Back to top
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.