Java Quiz-1

Approved & Edited by ProProfs Editorial Team
The editorial team at ProProfs Quizzes consists of a select group of subject experts, trivia writers, and quiz masters who have authored over 10,000 quizzes taken by more than 100 million users. This team includes our in-house seasoned quiz moderators and subject matter experts. Our editorial experts, spread across the world, are rigorously trained using our comprehensive guidelines to ensure that you receive the highest quality quizzes.
Learn about Our Editorial Process
| By Rahul Kumar
R
Rahul Kumar
Community Contributor
Quizzes Created: 3 | Total Attempts: 10,948
Questions: 100 | Attempts: 5,430

SettingsSettingsSettings
Java Quiz-1 - Quiz

Quiz created by Rahul


Questions and Answers
  • 1. 

    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.

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

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

    Rate this question:

  • 2. 

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

    • A.

      Option1

    • B.

      Option2

    • C.

      Option3

    • D.

      Option4

    Correct Answer(s)
    C. Option3
    D. Option4
  • 3. 

    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

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      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:

  • 4. 

    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

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

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

    Rate this question:

  • 5. 

    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

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      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:

  • 6. 

    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.

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

    Correct Answer
    C. 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:

  • 7. 

    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

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

    Correct Answer
    B. 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:

  • 8. 

    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

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

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

    Rate this question:

  • 9. 

    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

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

    Correct Answer
    D. 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:

  • 10. 

    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

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      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:

  • 11. 

    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

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

    Correct Answer
    C. 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:

  • 12. 

    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

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

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

    Rate this question:

  • 13. 

    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

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

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

    Rate this question:

  • 14. 

    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

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

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

    Rate this question:

  • 15. 

    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

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      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:

  • 16. 

    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

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

    Correct Answer
    C. 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:

  • 17. 

    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

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

    Correct Answer
    B. 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:

  • 18. 

    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

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

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

    Rate this question:

  • 19. 

    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.

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      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:

  • 20. 

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

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      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:

  • 21. 

    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

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      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:

  • 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

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      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: 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

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

    Correct Answer
    C. Option 3
  • 24. 

    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

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

    Correct Answer
    B. 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:

  • 25. 

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

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

    Correct Answer
    B. 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:

  • 26. 

    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

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

    Correct Answer
    C. 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:

  • 27. 

    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

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

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

    Rate this question:

  • 28. 

    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

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

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

    Rate this question:

  • 29. 

    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

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

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

    Rate this question:

  • 30. 

    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

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

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

    Rate this question:

  • 31. 

    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

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

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

    Rate this question:

  • 32. 

    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;

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

    Correct Answer(s)
    A. Option 1
    B. Option 2
    D. Option 4
    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.

    Rate this question:

  • 33. 

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

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

    Correct Answer
    D. 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:

  • 34. 

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

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      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:

  • 35. 

    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

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

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

    Rate this question:

  • 36. 

    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

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

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

    Rate this question:

  • 37. 

    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

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

    Correct Answer
    B. 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:

  • 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

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

    Correct Answer
    D. 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:

  • 39. 

    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

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

    Correct Answer
    C. 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:

  • 40. 

    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

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

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

    Rate this question:

  • 41. 

    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

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

    Correct Answer
    B. 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:

  • 42. 

    A constructor may return value including class type

    • A.

      True

    • B.

      False

    Correct Answer
    B. 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:

  • 43. 

    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

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      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:

  • 44. 

    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

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

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

    Rate this question:

  • 45. 

    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

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

    Correct Answer
    C. 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:

  • 46. 

    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.

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

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

    Rate this question:

  • 47. 

    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

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

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

    Rate this question:

  • 48. 

    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

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

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

    Rate this question:

  • 49. 

    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

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

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

    Rate this question:

  • 50. 

    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

    • A.

      Option 1

    • B.

      Option 2

    • C.

      Option 3

    • D.

      Option 4

    Correct Answer
    D. 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:

Quiz Review Timeline +

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.