Fresher Drive @ 27th September Java

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 Alokanibha
A
Alokanibha
Community Contributor
Quizzes Created: 4 | Total Attempts: 389
Questions: 21 | Attempts: 90

SettingsSettingsSettings
Fresher Drive @ 27th September Java - Quiz


Questions and Answers
  • 1. 

    What will be the output?public class Test {            static String  a;            static String  b;            public static void main(String ars[]){                        System.out.print(a+b);            }} 

    • A.

      Null

    • B.

      Compile time error

    • C.

      Nullnull

    • D.

      0

    Correct Answer
    C. Nullnull
    Explanation
    The output will be "nullnull" because both strings "a" and "b" are declared as static variables but not initialized with any values. In Java, when a string variable is not assigned a value, its default value is null. Therefore, when we concatenate two null strings, the result will be "nullnull".

    Rate this question:

  • 2. 

    You want a class to have access to members of another class in the same package. Which is the most restrictive access that accomplishes this objective?

    • A.

      Public

    • B.

      Private

    • C.

      Protected

    • D.

      Default access

    Correct Answer
    D. Default access
    Explanation
    Default access is the most restrictive access that allows a class to have access to members of another class in the same package. Default access, also known as package-private access, means that the members are accessible only within the same package. It restricts access from classes outside the package, providing a level of encapsulation and preventing unwanted access to the members. Public, private, and protected access modifiers have wider access scopes, allowing access from classes outside the package or subclasses, which may not be desired in this scenario.

    Rate this question:

  • 3. 

    Which is a valid declaration within an interface?

    • A.

      Public static short stop = 23

    • B.

      Protected short stop = 23

    • C.

      Transient short stop = 23

    • D.

      Final void madness(short stop)

    Correct Answer
    A. Public static short stop = 23
    Explanation
    The correct answer is "Public static short stop = 23". In an interface, all variables are by default public, static, and final. Therefore, it is valid to declare a public static short variable within an interface. The other options provided are not valid declarations within an interface.

    Rate this question:

  • 4. 

    What will be the output of the program?class Base{    Base()    {        System.out.print("Base");    }}public class Alpha extends Base{    public static void main(String[] args)    {        new Alpha();        new Base();    }}

    • A.

      Base

    • B.

      BaseBase

    • C.

      Compilation fails

    • D.

      The code runs with no output

    Correct Answer
    B. BaseBase
    Explanation
    The output of the program will be "BaseBase".

    This is because the class "Alpha" extends the class "Base", which means it inherits all the members of the "Base" class. When the program creates a new instance of "Alpha", it calls the constructor of the "Base" class, which prints "Base". Then, when the program creates a new instance of "Base", it again calls the constructor of the "Base" class, resulting in another "Base" being printed. Therefore, the output is "BaseBase".

    Rate this question:

  • 5. 

    What will be the output of the program?class Equals    public static void main(String [] args)    {        int x = 100;        double y = 100.1;        boolean b = (x = y);        System.out.println(b);    } 

    • A.

      True

    • B.

      False

    • C.

      Compilation fails

    • D.

      An exception is thrown at runtime

    Correct Answer
    C. Compilation fails
    Explanation
    The program will fail to compile because the expression `(x = y)` is attempting to assign a double value to an int variable, which is not allowed in Java. The assignment operator `=` can only be used to assign values of the same type.

    Rate this question:

  • 6. 

    What will be the output of the program?for(int i = 0; i < 3; i++){    switch(i)    {        case 0: break;        case 1: System.out.print("one ");        case 2: System.out.print("two ");        case 3: System.out.print("three ");    }System.out.printIn("done");

    • A.

      Done

    • B.

      One two done

    • C.

      One two three done

    • D.

      One two three two three done

    Correct Answer
    D. One two three two three done
    Explanation
    The program uses a switch statement inside a for loop.

    In the first iteration of the loop, when i is 0, the case 0 is encountered and the break statement is executed, causing the program to exit the switch statement. The program then prints "done".

    In the second iteration, when i is 1, the case 1 is encountered and "one " is printed. Since there is no break statement, the program falls through to the next case. The case 2 is encountered and "two " is printed. The program then exits the switch statement and prints "done".

    In the third iteration, when i is 2, the case 2 is encountered and "two " is printed. The program falls through to the next case and encounters case 3, printing "three ". The program then exits the switch statement and prints "done".

    Therefore, the output of the program is "one two three two three done".

    Rate this question:

  • 7. 

    Which is a valid keyword in java?

    • A.

      Interface

    • B.

      String

    • C.

      Float

    • D.

      Unsigned

    Correct Answer
    A. Interface
    Explanation
    The correct answer is "interface." In Java, "interface" is a valid keyword used to define a collection of abstract methods. It is used to define a contract that a class must implement, specifying the methods that the class should have. It allows for multiple inheritance and is commonly used for achieving abstraction and loose coupling in object-oriented programming.

    Rate this question:

  • 8. 

    Which method must be defined by a class implementing the java.lang.Runnable interface?

    • A.

      Void run()

    • B.

      Public void run()

    • C.

      Public void start()

    • D.

      Void run(int priority)

    Correct Answer
    B. Public void run()
    Explanation
    The method that must be defined by a class implementing the java.lang.Runnable interface is "public void run()". This method is responsible for the actual execution of the thread when it is started. It contains the code that will be run in a separate thread of execution.

    Rate this question:

  • 9. 

    Which interface does java.util.Hashtable implement?

    • A.

      Java.util.Map

    • B.

      Java.util.List

    • C.

      Java.util.HashTable

    • D.

      Java.util.Collection

    Correct Answer
    A. Java.util.Map
    Explanation
    The correct answer is Java.util.Map. Hashtable in Java implements the Map interface, which means it provides key-value pair storage and retrieval functionality. The Map interface allows you to store and retrieve values based on a unique key. Hashtable is a synchronized implementation of the Map interface, which means it is thread-safe and can be used in multi-threaded environments.

    Rate this question:

  • 10. 

    Which class cannot be a subclass in java?

    • A.

      Abstract class

    • B.

      Parent class

    • C.

      Final class

    • D.

      None of above

    Correct Answer
    C. Final class
    Explanation
    A final class in Java cannot be a subclass because the final keyword indicates that the class cannot be extended or inherited by any other class. It is used to prevent further modification or extension of the class. Therefore, a final class is the correct answer as it cannot be a superclass or a parent class for any other class.

    Rate this question:

  • 11. 

    Why we use array as a parameter of main method?

    • A.

      It is syntax

    • B.

      Can store multiple values

    • C.

      Both of above

    • D.

      None of above

    Correct Answer
    B. Can store multiple values
    Explanation
    The main method is the entry point of a Java program, and it is typically used to accept command-line arguments. By using an array as a parameter of the main method, we can pass multiple values as command-line arguments to the program. The array allows us to store and access these values easily within the program. Therefore, using an array as a parameter of the main method allows us to store multiple values effectively.

    Rate this question:

  • 12. 

    What will be the output of the program?public class Foo{     public static void main(String[] args)    {        try        {            return;        }        finally        {            System.out.println( "Finally" );        }    }} 

    • A.

      Finally

    • B.

      Compilation fails

    • C.

      The code runs with no output

    • D.

      An exception is thrown at runtime

    Correct Answer
    A. Finally
    Explanation
    The output of the program will be "Finally". This is because the code inside the finally block will always execute, regardless of whether there is an exception or a return statement in the try block. In this case, the return statement will cause the program to exit the main method, but before doing so, the finally block will be executed and "Finally" will be printed.

    Rate this question:

  • 13. 

    Which statement is true about a static nested class?

    • A.

      You must have a reference to an instance of the enclosing class in order to instantiate it

    • B.

      It does not have access to nonstatic members of the enclosing class

    • C.

      It's variables and methods must be static

    • D.

      It must extend the enclosing class

    Correct Answer
    B. It does not have access to nonstatic members of the enclosing class
    Explanation
    A static nested class is a nested class that is declared with the "static" keyword. This means that it can be accessed without creating an instance of the enclosing class. However, because it is static, it does not have access to nonstatic members (variables and methods) of the enclosing class. It can only access static members of the enclosing class. Therefore, the statement "It does not have access to nonstatic members of the enclosing class" is true.

    Rate this question:

  • 14. 

    Which three are methods of the Object class?   notify();   notifyAll();   isInterrupted();   synchronized();   interrupt();   wait(long msecs);   sleep(long msecs);   yield(); 

    • A.

      1, 2, 4

    • B.

      2, 4, 5

    • C.

      1, 2, 6

    • D.

      2, 3, 4

    Correct Answer
    C. 1, 2, 6
    Explanation
    The correct answer is 1, 2, 6. The methods notify(), notifyAll(), and wait(long msecs) are methods of the Object class. These methods are used for inter-thread communication and synchronization. The method notify() wakes up a single thread that is waiting on the object's monitor, while notifyAll() wakes up all the threads that are waiting on the object's monitor. The method wait(long msecs) causes the current thread to wait until either another thread invokes the notify() method or a specified amount of time has elapsed.

    Rate this question:

  • 15. 

    Which cause a compiler error?

    • A.

      Int[ ] scores = {3, 5, 7};

    • B.

      Int [ ][ ] scores = {2,7,6}, {9,3,45};

    • C.

      String cats[ ] = {"Fluffy", "Spot", "Zeus"};

    • D.

      Boolean results[ ] = new boolean [] {true, false, true};

    • E.

      Integer results[ ] = {new Integer(3), new Integer(5), new Integer(8)};

    Correct Answer
    B. Int [ ][ ] scores = {2,7,6}, {9,3,45};
    Explanation
    The line "int [ ][ ] scores = {2,7,6}, {9,3,45};" causes a compiler error because the syntax for initializing a 2D array is incorrect. The correct syntax for initializing a 2D array is "int[][] scores = {{2,7,6}, {9,3,45}};". The correct syntax uses double braces to enclose each row of the 2D array.

    Rate this question:

  • 16. 

    Which method executes only once?

    • A.

      Start() method

    • B.

      Init() method

    • C.

      Stop() method

    • D.

      Destroy() method

    Correct Answer
    B. Init() method
    Explanation
    The init() method executes only once. This method is called when the object is first created and initialized. It is typically used to initialize variables and perform any necessary setup tasks before the object is used. Once the init() method is executed, it will not be called again unless the object is re-initialized.

    Rate this question:

  • 17. 

    Which is a valid keyword in java? 

    • A.

      Interface

    • B.

      String

    • C.

      Float

    • D.

      Unsigned

    Correct Answer
    A. Interface
    Explanation
    The keyword "interface" is a valid keyword in Java. It is used to define a collection of abstract methods that can be implemented by a class. Interfaces provide a way to achieve multiple inheritance in Java and allow classes to have common behavior without actually being related through inheritance.

    Rate this question:

  • 18. 

    Which class cannot be a subclass in java?

    • A.

      Abstract class

    • B.

      Parent class

    • C.

      Final class

    • D.

      None of above

    Correct Answer
    C. Final class
    Explanation
    A final class cannot be a subclass in Java because the final keyword is used to restrict the inheritance of a class. When a class is declared as final, it means that it cannot be extended or subclassed by any other class. This is done to prevent any further modifications or extensions to the class, ensuring that its functionality remains intact and unchanged. Therefore, a final class cannot serve as a superclass or be extended by any other class.

    Rate this question:

  • 19. 

    What will be the output of the program?public class Foo{     public static void main(String[] args)    {        try        {            return;        }        finally        {            System.out.println( "Finally" );        }    }} 

    • A.

      Finally

    • B.

      Compilation fails

    • C.

      The code runs with no output

    • D.

      An exception is thrown at runtime

    Correct Answer
    A. Finally
    Explanation
    The output of the program will be "Finally". This is because the code is using a try-finally block. In this case, the try block does not have any code that will be executed, as it only contains a return statement. However, the finally block will always be executed, regardless of whether an exception is thrown or not. Therefore, "Finally" will be printed as the output of the program.

    Rate this question:

  • 20. 

    Which statement is true about a static nested class? 

    • A.

      You must have a reference to an instance of the enclosing class in order to instantiate it.

    • B.

      It does not have access to nonstatic members of the enclosing class

    • C.

      It's variables and methods must be static

    • D.

      It must extend the enclosing class

    Correct Answer
    B. It does not have access to nonstatic members of the enclosing class
    Explanation
    A static nested class is a class that is defined inside another class, but it is marked as static. This means that it can be accessed without creating an instance of the enclosing class. However, because it is static, it does not have access to the nonstatic members (variables and methods) of the enclosing class. Therefore, the statement "It does not have access to nonstatic members of the enclosing class" is true.

    Rate this question:

  • 21. 

     Which cause a compiler error? 

    • A.

      Int[ ] scores = {3, 5, 7};

    • B.

      Int [ ][ ] scores = {2,7,6}, {9,3,45};

    • C.

      String cats[ ] = {"Fluffy", "Spot", "Zeus"};

    • D.

      Boolean results[ ] = new boolean [] {true, false, true};

    • E.

      Integer results[ ] = {new Integer(3), new Integer(5), new Integer(8)};

    Correct Answer
    B. Int [ ][ ] scores = {2,7,6}, {9,3,45};
    Explanation
    The line "int [ ][ ] scores = {2,7,6}, {9,3,45};" causes a compiler error because the array initialization is incorrect. In a two-dimensional array, each row should be enclosed in curly braces. So the correct initialization should be "int [ ][ ] scores = {{2,7,6}, {9,3,45}};".

    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
  • Dec 08, 2023
    Quiz Edited by
    ProProfs Editorial Team
  • Sep 26, 2014
    Quiz Created by
    Alokanibha
Back to Top Back to top
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.