Java Fundamentals

Reviewed by Editorial Team
The ProProfs editorial team is comprised of experienced subject matter experts. They've collectively created over 10,000 quizzes and lessons, serving over 100 million users. Our team includes in-house content moderators and subject matter experts, as well as a global network of rigorously trained contributors. All adhere to our comprehensive editorial guidelines, ensuring the delivery of high-quality content.
Learn about Our Editorial Process
| By AARATHI_123
A
AARATHI_123
Community Contributor
Quizzes Created: 1 | Total Attempts: 733
| Attempts: 733 | Questions: 25
Please wait...
Question 1 / 25
0 %
0/100
Score 0/100
1. Which of the following may be part of a class definition?

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.

Submit
Please wait...
About This Quiz
Java Quizzes & Trivia

This JAVA FUNDAMENTALS quiz assesses key concepts in Java programming, including file naming, class construction, string manipulation, and object instantiation. It is designed for learners to validate their... see moreunderstanding and skills in Java development. see less

2. String river = new String("Columbia"); System.out.println(river.length());

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.

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

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.

Submit
4. Which one is a valid declaration of a boolean?

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.

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

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".

Submit
6. Which of the following is TRUE?

Explanation

A class in Java always has a constructor, which may be automatically provided by the Java compiler.

Submit
7. 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 ");
        }
    }
}

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.

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

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.

Submit
9. What is byte code in the context of Java?

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.

Submit
10. What is garbage collection in the context of Java?

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.

Submit
11.
Which will legally declare, construct, and initialize an array?
   
   
   
   

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.

Submit
12. Which is a reserved word in the Java programming language?

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.

Submit
13. What is different between a Java applet and a Java application?

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

Submit
14. What will be the output of the program?
public class Foo 
{  
    public static void main(String[] args) 
    {
        try 
        { 
            return; 
        } 
        finally 
        {
            System.out.println( "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.

Submit
15. Which is a valid keyword in java?

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.

Submit
16. Which of the following will directly stop the execution of a Thread?

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.

Submit
17. 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);
    }
}

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.

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

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.

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

Explanation

not-available-via-ai

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

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.

Submit
21.
public interface Foo 
{ 
    int k = 4; /* Line 3 */
}

Which three piece of codes are equivalent to line 3?

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.

Submit
22. Which two are valid constructors for 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.

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

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.

Submit
24. 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");

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.

Submit
25. Which cannot directly cause a thread to stop executing?

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.

Submit
View My Results

Quiz Review Timeline (Updated): Mar 19, 2023 +

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
Cancel
  • All
    All (25)
  • Unanswered
    Unanswered ()
  • Answered
    Answered ()
Which of the following may be part of a class definition?
String river = new String("Columbia");...
Public class MyClass{...
Which one is a valid declaration of a boolean?
Import myLibrary.*;...
Which of the following is TRUE?
What will be the output of the program?...
You read the following statement in a Java program that compiles...
What is byte code in the context of Java?
What is garbage collection in the context of Java?
Which will legally declare, construct, and initialize an array? ...
Which is a reserved word in the Java programming language?
What is different between a Java applet and a Java application?
What will be the output of the program?...
Which is a valid keyword in java?
Which of the following will directly stop the execution of a Thread?
What will be the output of the program?...
What is the name of the method used to start a thread execution?
Which class does not override the equals() and...
Which two of the following methods are defined in class Thread?
Public interface Foo ...
Which two are valid constructors for Thread?
You need to store elements in a collection that guarantees that no...
What will be the output of the program? ...
Which cannot directly cause a thread to stop executing?
Alert!

Advertisement