Java Quiz-1

100 Questions | Total Attempts: 829

SettingsSettingsSettings
Please wait...
Java Quizzes & Trivia

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

  • 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

  • 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

  • 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

  • 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

  • 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

  • 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

  • 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

  • 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

  • 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

  • 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

  • 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

  • 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

  • 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

  • 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

  • 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

  • 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

  • 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

  • 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

  • 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

  • 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

  • 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

  • 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

  • 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

  • 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

Back to Top Back to top