Java Fundamentals

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 AARATHI_123
A
AARATHI_123
Community Contributor
Quizzes Created: 1 | Total Attempts: 729
Questions: 25 | Attempts: 729

SettingsSettingsSettings
Java Quizzes & Trivia

Basics of Java


Questions and Answers
  • 1. 

    Import myLibrary.*; public class ShowSomeClass { // code for the class... }  What is the name of the java file containing this program?

    • A.

      MyLibrary.java

    • B.

      ShowSomeClass.java

    • C.

      ShowSomeClass

    • D.

      ShowSomeClass.class

    Correct Answer
    B. ShowSomeClass.java
    Explanation
    The name of the java file containing this program is "ShowSomeClass.java" because in Java, the name of the file must match the name of the public class defined within it. In this case, the public class is "ShowSomeClass", so the file should be named "ShowSomeClass.java".

    Rate this question:

  • 2. 

    Which of the following is TRUE?

    • A.

      In java, an instance field declared public generates a compilation error

    • B.

      Int is the name of a class available in the package java.lang

    • C.

      Instance variable names may only contain letters and digits.

    • D.

      A class has always a constructor (possibly automatically supplied by the java compiler)

    Correct Answer
    D. A class has always a constructor (possibly automatically supplied by the java compiler)
    Explanation
    A class in Java always has a constructor, which may be automatically provided by the Java compiler.

    Rate this question:

  • 3. 

    String river = new String(“Columbia”); System.out.println(river.length());

    • A.

      6

    • B.

      Columbia

    • C.

      8

    • D.

      River

    Correct Answer
    C. 8
    Explanation
    The code creates a new String object named "river" with the value "Columbia". The length() method is then called on the "river" object, which returns the number of characters in the string. In this case, the string "Columbia" has 8 characters, so the output of the code will be 8.

    Rate this question:

  • 4. 

    Which of the following may be part of a class definition?

    • A.

      Instance variables

    • B.

      Instance methods

    • C.

      Constructors

    • D.

      All of the above

    Correct Answer
    D. All of the above
    Explanation
    All of the options listed, which include instance variables, instance methods, and constructors, can be part of a class definition. Instance variables represent the attributes or properties of an object, instance methods define the behavior or actions that the object can perform, and constructors are special methods used to initialize objects of the class. Therefore, all of these components are commonly included in a class definition.

    Rate this question:

  • 5. 

    What is different between a Java applet and a Java application?

    • A.

      An application can in general be trusted whereas an applet can't

    • B.

      An applet must be executed in a browser environment.

    • C.

      An applet is not able to access the files of the computer it runs on

    • D.

      (A), (B) and (C).

    Correct Answer
    D. (A), (B) and (C).
    Explanation
    A Java applet and a Java application differ in several ways. Firstly, an application can generally be trusted, whereas an applet cannot. This means that an application has more privileges and can perform actions that an applet is restricted from doing. Secondly, an applet must be executed in a browser environment. It is designed to run within a web page and is subject to certain security restrictions imposed by the browser. Lastly, an applet is not able to access the files of the computer it runs on. This limitation ensures that applets cannot access or modify sensitive files on the user's machine. Therefore, the correct answer is (A), (B), and (C).

    Rate this question:

  • 6. 

    Public class MyClass{ public MyClass(){/*code*/} // more code... } To instantiate MyClass, you would write?

    • A.

      MyClass mc = new MyClass();

    • B.

      MyClass mc = MyClass();

    • C.

      MyClass mc = MyClass;

    • D.

      It can't be done. The constructor of MyClass should be defined as public void MyClass(){/*code*/}

    Correct Answer
    A. MyClass mc = new MyClass();
    Explanation
    To instantiate an object of the MyClass class, the correct syntax is "MyClass mc = new MyClass();". This creates a new instance of the MyClass class and assigns it to the variable "mc". The "new" keyword is used to allocate memory for the object and invoke the constructor of the class to initialize its state.

    Rate this question:

  • 7. 

    What is byte code in the context of Java?

    • A.

      The type of code generated by a Java compiler

    • B.

      The type of code generated by a Java Virtual Machine

    • C.

      It is the code written within the instance methods of a class.

    • D.

      It is another name for comments written within a program.

    Correct Answer
    A. The type of code generated by a Java compiler
    Explanation
    Byte code refers to the type of code generated by a Java compiler. Java source code is compiled into byte code, which is a low-level representation of the code that can be executed by a Java Virtual Machine (JVM). This byte code is platform-independent and can be run on any device or operating system that has a JVM installed. The JVM then interprets and executes the byte code, making Java a portable language.

    Rate this question:

  • 8. 

    What is garbage collection in the context of Java?

    • A.

      The operating system periodically deletes all of the java files available on the system

    • B.

      Any package imported in a program and not used is automatically deleted

    • C.

      When all references to an object are gone, the memory used by the object is automatically reclaimed.

    • D.

      . The JVM checks the output of any Java program and deletes anything that doesn't make sense.

    Correct Answer
    C. When all references to an object are gone, the memory used by the object is automatically reclaimed.
    Explanation
    Garbage collection in the context of Java refers to the automatic process of reclaiming memory used by objects that are no longer referenced in a program. When all references to an object are gone, the memory occupied by that object is automatically freed up by the Java Virtual Machine (JVM). This helps in managing memory efficiently and prevents memory leaks.

    Rate this question:

  • 9. 

    You read the following statement in a Java program that compiles and  executes.       submarine.dive(depth); What can you say for sure?

    • A.

      Depth must be an int

    • B.

      Dive must be a method

    • C.

      Dive must be the name of an instance field.

    • D.

      Submarine must be the name of a class

    Correct Answer
    B. Dive must be a method
    Explanation
    Based on the given statement in the Java program, "submarine.dive(depth);", we can say for sure that "dive" must be a method. This is because the statement follows the syntax for calling a method in Java, where "submarine" is the object or instance on which the method "dive" is being called, and "depth" is a parameter being passed to the method. Therefore, "dive" must be a method that is defined within the class to which the "submarine" object belongs.

    Rate this question:

  • 10. 

    Which will legally declare, construct, and initialize an array?                

    • A.

      Int [] myList = {"1", "2", "3"};

    • B.

      Int [] myList = (5, 8, 2);

    • C.

      Int myList [] [] = {4,9,7,0};

    • D.

      Int myList [] = {4, 3, 7};

    Correct Answer
    D. Int myList [] = {4, 3, 7};
    Explanation
    The correct answer is "int myList [] = {4, 3, 7}". This statement declares, constructs, and initializes an array named "myList" with the values 4, 3, and 7. The array is of type int and the square brackets indicate that it is an array. The values are enclosed in curly braces and separated by commas, which is the syntax for initializing an array in Java.

    Rate this question:

  • 11. 

    Which is a reserved word in the Java programming language?

    • A.

      Method

    • B.

      Native

    • C.

      Subclass

    • D.

      Reference

    Correct Answer
    B. Native
    Explanation
    In the Java programming language, "native" is a reserved word. Reserved words are predefined keywords that have special meanings and cannot be used as identifiers (such as variable names or method names) in the program. The "native" keyword is used to indicate that a method is implemented in a language other than Java, typically in a native language like C or C++. This allows for the integration of platform-specific code into Java programs.

    Rate this question:

  • 12. 

    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 that is used to define a collection of abstract methods. It is used to achieve abstraction and multiple inheritance in Java. Interfaces can be implemented by classes to provide specific implementations for the abstract methods defined in the interface.

    Rate this question:

  • 13. 

    public interface Foo { int k = 4; /* Line 3 */ } Which three piece of codes are equivalent to line 3?

    • A.

      Final int k = 4;

    • B.

      Public int k = 4;

    • C.

      Static int k = 4;

    • D.

      Abstract int k = 4;

    • E.

      Volatile int k = 4;

    Correct Answer(s)
    A. Final int k = 4;
    B. Public int k = 4;
    C. Static int k = 4;
    Explanation
    The correct answer is final int k = 4; because when a variable is declared as final, its value cannot be changed once it is assigned. In this case, the variable "k" is declared as final and assigned the value of 4, making it a constant. The other options, public int k = 4; and static int k = 4; are not equivalent because they do not restrict the variable from being changed.

    Rate this question:

  • 14. 

    Which one is a valid declaration of a boolean?

    • A.

      Boolean b1 = 0;

    • B.

      Boolean b2 = 'false';

    • C.

      Boolean b3 = false;

    • D.

      Boolean b4 = Boolean.false();

    Correct Answer
    C. Boolean b3 = false;
    Explanation
    The correct answer is "boolean b3 = false;" because in Java, the boolean data type can only have two possible values: true or false. The other options are incorrect because "0" is not a valid boolean value, "'false'" is a string and not a boolean value, and there is no method called "Boolean.false()" to declare a boolean.

    Rate this question:

  • 15. 

    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 program will output "Finally". In this program, the code inside the finally block will always execute, regardless of whether there is a return statement in the try block. So, even though the return statement is encountered, the finally block will still execute and print "Finally" before the program terminates.

    Rate this question:

  • 16. 

    What will be the output of the program? try { int x = 0; int y = 5 / x; } catch (Exception e) { System.out.println("Exception"); } catch (ArithmeticException ae) { System.out.println(" Arithmetic Exception"); } System.out.println("finished");

    • A.

      Finished

    • B.

      Exception

    • C.

      Compilation fails.

    • D.

      Arithmetic Exception

    Correct Answer
    C. Compilation fails.
    Explanation
    The program will fail to compile because the catch block for the ArithmeticException is placed after the catch block for the more general Exception. In Java, catch blocks must be ordered from most specific to most general, so the catch block for ArithmeticException should come before the catch block for Exception.

    Rate this question:

  • 17. 

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

    • A.

      Nothing. The program will not compile because no exceptions are specified.

    • B.

      Nothing. The program will not compile because no catch clauses are specified.

    • C.

      Hello world.

    • D.

      Hello world Finally executing

    Correct Answer
    D. Hello world Finally executing
    Explanation
    The program will output "Hello world Finally executing". This is because the try block is executed first, which prints "Hello world". Then, the finally block is executed, which prints "Finally executing". The finally block is always executed, regardless of whether an exception is thrown or not.

    Rate this question:

  • 18. 

    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); /* Line 7 */ 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 there is a type mismatch in line 7. The variable "x" is of type "int" while the variable "y" is of type "double". In Java, a boolean expression cannot be assigned the value of a double. Therefore, the line "boolean b = (x = y);" will cause a compilation error.

    Rate this question:

  • 19. 

    Which class does not override the equals() and hashcode() methods, inheriting them directly from class Object?

    • A.

      Java.lang.String

    • B.

      Java.lang.Double

    • C.

      Java.lang.StringBuffer

    • D.

      Java.lang.Character

    Correct Answer
    C. Java.lang.StringBuffer
  • 20. 

    You need to store elements in a collection that guarantees that no duplicates are stored. Which one of the following interfaces provide that capability?

    • A.

      Java.util.Map

    • B.

      Java.util.List

    • C.

      Java.util.Collection

    • D.

      None of the above

    Correct Answer
    A. Java.util.Map
    Explanation
    The correct answer is Java.util.Map. The Map interface in Java provides a collection that guarantees that no duplicates are stored. It is an associative data structure that stores key-value pairs, where each key is unique. If an attempt is made to add a duplicate key, the existing value associated with that key will be replaced. Therefore, using a Map ensures that no duplicate keys are stored.

    Rate this question:

  • 21. 

    What is the name of the method used to start a thread execution?

    • A.

      Init();

    • B.

      Start();

    • C.

      Run();

    • D.

      Resume();

    Correct Answer
    B. Start();
    Explanation
    The start() method is used to start the execution of a thread. When the start() method is called, it creates a new thread and invokes the run() method of the thread. The run() method contains the code that will be executed in the new thread. Therefore, start() is the correct method to start a thread execution. The init() method is used to initialize a thread, resume() method is used to resume a suspended thread, and run() method is the entry point for the thread's code.

    Rate this question:

  • 22. 

    Which two are valid constructors for Thread?

    • A.

      Thread(Runnable r, String name)

    • B.

      Thread()

    • C.

      Thread(int priority)

    • D.

      Thread(Runnable r, ThreadGroup g)

    Correct Answer(s)
    A. Thread(Runnable r, String name)
    B. Thread()
    Explanation
    The question asks for the valid constructors for the Thread class. The first constructor, Thread(Runnable r, String name), takes a Runnable object and a String as parameters. This constructor is used to create a new thread with a specific name. The second constructor, Thread(), is a default constructor that creates a new thread with no parameters. Both of these constructors are valid ways to create a new Thread object.

    Rate this question:

  • 23. 

    Which cannot directly cause a thread to stop executing?

    • A.

      Calling the SetPriority() method on a Thread object.

    • B.

      Calling the wait() method on an object.

    • C.

      Calling notify() method on an object.

    • D.

      Calling read() method on an InputStream object.

    Correct Answer
    C. Calling notify() method on an object.
    Explanation
    Calling the notify() method on an object cannot directly cause a thread to stop executing. The notify() method is used to wake up a single thread that is waiting on the object's monitor. It does not stop the execution of the thread itself. The other options mentioned, such as calling the SetPriority() method on a Thread object, calling the wait() method on an object, or calling the read() method on an InputStream object, can potentially affect the execution of a thread.

    Rate this question:

  • 24. 

    Which of the following will directly stop the execution of a Thread?

    • A.

      Wait()

    • B.

      Notify()

    • C.

      Notifyall()

    • D.

      Exits synchronized code

    Correct Answer
    A. Wait()
    Explanation
    The wait() method is used to make a thread wait until another thread notifies it to resume. When a thread calls the wait() method, it releases the lock it holds, allowing other threads to execute. This means that the execution of the thread calling wait() is paused until it receives a notification. Therefore, wait() directly stops the execution of a thread until it is notified to continue.

    Rate this question:

  • 25. 

    Which two of the following methods are defined in class Thread?

    • A.

      Start()

    • B.

      Notify()

    • C.

      Run()

    • D.

      Terminate()

    Correct Answer(s)
    A. Start()
    C. Run()
    Explanation
    The methods start() and run() are defined in the class Thread. The start() method is used to start a new thread and call the run() method. The run() method contains the code that will be executed when the thread is started. The notify() method is not defined in the class Thread and the terminate() method is not a valid method in the class Thread.

    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
  • Mar 19, 2023
    Quiz Edited by
    ProProfs Editorial Team
  • Nov 15, 2011
    Quiz Created by
    AARATHI_123
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.