Can You Pass This Tough Java Test?

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 Volt02
V
Volt02
Community Contributor
Quizzes Created: 1 | Total Attempts: 1,535
Questions: 39 | Attempts: 1,538

SettingsSettingsSettings
Can You Pass This Tough Java Test? - Quiz

JAVA FINAL EXAM


Questions and Answers
  • 1. 

    What is the size of a Char?

    • A.

      4 bits

    • B.

      7 bits

    • C.

      8 bits

    • D.

      16 bits

    Correct Answer
    D. 16 bits
    Explanation
    The size of a char in most programming languages is typically 8 bits, which allows it to store a single character. However, in some languages like Java, a char is 16 bits in size, allowing it to store a wider range of characters including Unicode characters. Therefore, the correct answer is 16 bits.

    Rate this question:

  • 2. 

     A class cannot be declared

    • A.

      Static

    • B.

      Private

    Correct Answer
    B. Private
    Explanation
    A class cannot be declared as private because the private access modifier is used to restrict access to members (variables and methods) within a class, not to the class itself. The private access modifier is used to make members accessible only within the same class and not from outside the class. However, a class can be declared as static, which means it can be accessed without creating an instance of the class.

    Rate this question:

  • 3. 

    Following code will result in: int a = 3.5;

    • A.

      Compilation error

    • B.

      Runtime error

    Correct Answer
    A. Compilation error
    Explanation
    The code will result in a compilation error because it is trying to assign a decimal value (3.5) to an integer variable (a). In Java, the type of the variable must match the type of the value being assigned to it, or it must be a compatible type. Since an integer cannot store decimal values, the code will not compile.

    Rate this question:

  • 4. 

    Following code will result in: int a1 = 5; double a2 = (float)a1;

    • A.

      Runtime error

    • B.

      No errors

    Correct Answer
    B. No errors
    Explanation
    The code will not result in any errors because it is a valid type conversion. The value of the integer variable a1 is being casted to a float and then assigned to the double variable a2. This type of conversion is allowed and will not cause any runtime errors.

    Rate this question:

  • 5. 

     Following code will result in: int a = 9/0;

    • A.

      Compilation error: Divisions must be in a try block.

    • B.

      Runtime Exception

    Correct Answer
    B. Runtime Exception
    Explanation
    The code will result in a runtime exception because it is attempting to divide a number by zero, which is not allowed in mathematics. This will cause a runtime error and the program will terminate abruptly.

    Rate this question:

  • 6. 

    A class can be transient

    • A.

      True

    • B.

      False

    Correct Answer
    B. False
    Explanation
    A class cannot be transient. The "transient" keyword in programming languages like Java is used to indicate that a variable should not be serialized when an object is being converted into a stream of bytes. However, the "transient" keyword cannot be applied to a class itself, only to its variables. Therefore, the statement that a class can be transient is false.

    Rate this question:

  • 7. 

    Following code will result in: class A { int b = 1; public static void main(String [] args) { System.out.println("b is " + b); }}

    • A.

      Compilation error

    • B.

      Runtime Error

    Correct Answer
    A. Compilation error
    Explanation
    The code will result in a compilation error because the variable "b" is a non-static variable and cannot be referenced from a static context. In the main method, which is a static method, the variable "b" is being accessed without an instance of the class A. To fix this error, the variable "b" should be declared as static or an instance of class A should be created to access the variable.

    Rate this question:

  • 8. 

    Following code will result in: class A { public static void main(String [] args) {A a = new B(); }} class B extends A {}

    • A.

      Compile error

    • B.

      No error

    Correct Answer
    B. No error
    Explanation
    The code will compile and run without any errors, but it will not produce any output. This is because the main method in class A does not have any statements that print or return anything. The main method creates an object of class B and assigns it to a reference variable of type A. This is possible because class B is a subclass of class A, and Java allows upcasting from a subclass to a superclass. However, since the main method does not do anything with the object a, the program ends after creating it.

    Rate this question:

  • 9. 

    Following code will result in: class A { public static void main(String [] args) {A a = new B(); }} class B extends A {}

    • A.

      Runtime Exception

    • B.

      No errors

    Correct Answer
    B. No errors
    Explanation
    The code will not result in any errors because class B extends class A, so creating an object of class B and assigning it to a variable of type A is valid. This is known as polymorphism in Java.

    Rate this question:

  • 10. 

    Methods that are marked protected can be called in any subclass of that class.

    • A.

      True

    • B.

      False

    Correct Answer
    A. True
    Explanation
    Protected methods in a class can be accessed by any subclass of that class. This means that subclasses inherit the protected methods from their superclass and can call them as if they were their own methods. This allows for code reuse and promotes encapsulation, as subclasses can access and utilize the functionality provided by the protected methods. Therefore, the statement is true.

    Rate this question:

  • 11. 

    An abstract class can have non-abstract methods

    • A.

      True

    • B.

      False

    Correct Answer
    A. True
    Explanation
    An abstract class can have non-abstract methods because an abstract class is a class that cannot be instantiated and is meant to be inherited by other classes. It can contain both abstract and non-abstract methods. Non-abstract methods in an abstract class can have a complete implementation, providing default behavior that can be inherited by its subclasses. This allows the abstract class to define common functionality that can be shared among its subclasses, while still allowing each subclass to implement its own unique behavior.

    Rate this question:

  • 12. 

    Java keywords are written in lowercase as well as uppercase

    • A.

      True

    • B.

      False

    Correct Answer
    B. False
    Explanation
    In Java, keywords are written in lowercase. Java is a case-sensitive language, which means that uppercase and lowercase letters are treated differently. Keywords such as "public," "class," "void," and "if" are all written in lowercase. Using uppercase letters for these keywords will result in a compilation error. Therefore, the statement "Java keywords are written in lowercase as well as uppercase" is false.

    Rate this question:

  • 13. 

    What is an instanceof

    • A.

      A methods in object

    • B.

      An operator and keyword

    Correct Answer
    B. An operator and keyword
    Explanation
    The correct answer is that instanceof is both an operator and a keyword. It is used to check if an object belongs to a certain class or if it is an instance of a specific class. As an operator, it returns a boolean value indicating whether the object is an instance of the specified class or one of its subclasses. As a keyword, it is used in the syntax of the instanceof operator to perform the type checking.

    Rate this question:

  • 14. 

    Primitive datatypes are allocated on a stack

    • A.

      True

    • B.

      False

    Correct Answer
    A. True
    Explanation
    Primitive datatypes are allocated on a stack because they are simple and small data types that do not require a large amount of memory. The stack is a region of memory that is used for local variables and function calls. When a primitive datatype is declared, it is allocated on the stack and the memory is automatically freed when the variable goes out of scope. This allows for efficient memory management and quick access to the data.

    Rate this question:

  • 15. 

    Can you compare a boolean to an integer?

    • A.

      Yes

    • B.

      No

    Correct Answer
    B. No
    Explanation
    A boolean represents a binary value of either true or false, while an integer represents a numerical value. These two types are not directly comparable as they represent different concepts. Comparing a boolean to an integer would not make logical sense and would result in an error. Therefore, the correct answer is no.

    Rate this question:

  • 16. 

    If class A implements an interface does it need to implement all methods of that interface?

    • A.

      Yes, always

    • B.

      No, not when A is abstract

    Correct Answer
    B. No, not when A is abstract
    Explanation
    When a class A implements an interface, it is required to implement all the methods of that interface. However, when class A is declared as abstract, it is not mandatory to implement all the methods of the interface. Abstract classes can have abstract methods, which means that the responsibility of implementing those methods is delegated to the concrete subclasses of class A. Therefore, in this case, the class A can choose to implement only a subset of the methods defined in the interface, leaving the remaining methods to be implemented by its subclasses.

    Rate this question:

  • 17. 

    Integer a = new Integer(2); Integer b = new Integer(2); What happens when you do if (a==b)?

    • A.

      True

    • B.

      FALSE

    Correct Answer
    B. FALSE
    Explanation
    In Java, when comparing objects using the "==" operator, it checks if the two objects refer to the same memory location. In this case, the objects "a" and "b" are both Integer objects, but they are created using the "new" keyword, which means they are separate instances and therefore refer to different memory locations. Hence, the condition "a==b" will evaluate to false.

    Rate this question:

  • 18. 

    The methods wait(), notify() and notifyAll() in Object need to be called from synchronized pieces of code.

    • A.

      True

    • B.

      False

    Correct Answer
    A. True
    Explanation
    The methods wait(), notify(), and notifyAll() in the Object class need to be called from synchronized pieces of code because they are used for inter-thread communication and synchronization. When a thread calls wait(), it releases the lock on the object and waits for another thread to notify it. Similarly, when a thread calls notify() or notifyAll(), it notifies the waiting thread(s) to wake up and continue execution. To ensure thread safety and avoid race conditions, these methods should only be called within synchronized blocks or methods.

    Rate this question:

  • 19. 

    Inner classes can be defined within methods

    • A.

      True

    • B.

      False

    Correct Answer
    A. True
    Explanation
    Inner classes can be defined within methods. This means that a class can be declared inside a method of another class. This allows for more flexibility and encapsulation in the code structure. Inner classes have access to the variables and methods of the outer class, which can be useful in certain situations. This feature is available in languages like Java and C#.

    Rate this question:

  • 20. 

    Synchronized is a keyword to tell a Thread to grab an Object lock before continuing execution.

    • A.

      True

    • B.

      False

    Correct Answer
    A. True
    Explanation
    The explanation for the given correct answer is that the "synchronized" keyword in Java is used to achieve thread synchronization. When a method or a block of code is declared as synchronized, it means that only one thread can access that method or block at a time. It ensures that multiple threads do not interfere with each other and avoids race conditions. By grabbing an object lock, the synchronized thread ensures that it has exclusive access to the object before continuing its execution. Therefore, the statement "Synchronized is a keyword to tell a Thread to grab an Object lock before continuing execution" is true.

    Rate this question:

  • 21. 

    The default statement of a switch is always executed

    • A.

      True

    • B.

      False

    Correct Answer
    B. False
    Explanation
    The default statement of a switch is not always executed. The default statement is only executed when none of the cases match the value of the expression being evaluated in the switch statement. If a case matches the value, the corresponding block of code is executed and the switch statement is exited. If there is no default statement, and none of the cases match, then the switch statement does nothing.

    Rate this question:

  • 22. 

    How can you prevent a member variable from becoming serialized?

    • A.

      By marking it volatile

    • B.

      By marking it transient

    Correct Answer
    B. By marking it transient
    Explanation
    By marking a member variable as transient, it indicates to the serialization process that the variable should not be serialized. This means that when an object is serialized, the transient variable will not be included in the serialized output. This can be useful for variables that contain sensitive or unnecessary information that should not be persisted when the object is serialized and deserialized. On the other hand, marking a variable as volatile does not prevent it from being serialized. Volatile is used to indicate that a variable may be accessed by multiple threads and its value should always be read from and written to the main memory, rather than being cached in a thread's local memory.

    Rate this question:

  • 23. 

    What is Java (in regard to Computer Science) ?

    • A.

       A type of coffee

    • B.

      AN OBJECT-ORIENTED PROGRAMMING LANGUAGE

    Correct Answer
    B. AN OBJECT-ORIENTED PROGRAMMING LANGUAGE
    Explanation
    Java is an object-oriented programming language used in computer science. It was designed to be platform-independent, meaning that it can run on any operating system. Java is known for its simplicity, versatility, and security features. It is widely used for developing various applications, including web and mobile applications, enterprise software, and embedded systems. Java's object-oriented nature allows for the creation of reusable code components called objects, making it easier to develop and maintain complex software systems.

    Rate this question:

  • 24. 

    WHAT IS AN APPLET?

    • A.

      A JAVA PROGRAM THAT IS RUN THROUGH A WEB BROWSER

    • B.

       A type of coffee

    Correct Answer
    A. A JAVA PROGRAM THAT IS RUN THROUGH A WEB BROWSER
    Explanation
    An applet is a Java program that is designed to be run through a web browser. It is a small application that is embedded within a web page and can be executed by the Java Virtual Machine (JVM) in the user's web browser. Applets are used to add interactive features to web pages, such as animations, games, and data visualization. They are written in Java programming language and are platform-independent, meaning they can run on any device with a compatible web browser and Java support.

    Rate this question:

  • 25. 

    Java runs on _______.

    • A.

       Windows

    • B.

      All of the Above

    • C.

       Unix/Linux

    Correct Answer
    B. All of the Above
    Explanation
    Java is a programming language that is designed to be platform-independent, meaning it can run on multiple operating systems. It is not restricted to a specific operating system like Windows or Unix/Linux. Therefore, the correct answer is "All of the Above" as Java can run on Windows, Unix/Linux, and other operating systems as well.

    Rate this question:

  • 26. 

    Why can't the whole program just consist of the one line that does the painting ?

    • A.

      In Java, to create an applet, you can't call on functions without defining a class to call them.

    • B.

      The drawString function is not defined without the "import" statements at the top.

    • C.

       All of the above.

    Correct Answer
    C.  All of the above.
    Explanation
    The correct answer is "All of the above." In Java, to create an applet, you need to define a class to call functions. Without a class, you cannot call the drawString function or any other functions. Additionally, the drawString function is not defined without the necessary "import" statements at the top of the program. Therefore, all of the given statements are valid reasons why the whole program cannot just consist of the one line that does the painting.

    Rate this question:

  • 27. 

    What's the difference between an Applet and an application ?

    • A.

      Applets can paint words, applications cannot.

    • B.

       Applets are run over the web.

    Correct Answer
    B.  Applets are run over the web.
    Explanation
    Applets are small programs that are designed to be run within a web browser. They are typically used to add interactive features to web pages. On the other hand, applications are standalone programs that are installed and run directly on a computer or mobile device. They are not limited to running within a web browser and can access the full capabilities of the operating system. Therefore, the difference between an applet and an application is that applets are specifically designed to be run over the web, while applications are not limited to web-based environments.

    Rate this question:

  • 28. 

    What is the main function of any variable ?

    • A.

       To add numbers together

    • B.

       To keep track of data in the memory of the computer

    Correct Answer
    B.  To keep track of data in the memory of the computer
    Explanation
    The main function of any variable is to keep track of data in the memory of the computer. Variables are used to store and manipulate data during the execution of a program. They allow the program to store values, retrieve them, and perform operations on them. By assigning a value to a variable, the program can remember and access that value later on. This helps in maintaining and managing data throughout the program's execution.

    Rate this question:

  • 29. 

    What is the proper way to declare a variable ?

    • A.

      VariableName variableType;

    • B.

      VariableType variableName;

    Correct Answer
    B. VariableType variableName;
    Explanation
    The proper way to declare a variable is by specifying the variable type first, followed by the variable name. This convention is commonly used in programming languages to ensure clarity and consistency in code. By declaring the variable type first, it helps both the programmer and the compiler understand the type of data that will be stored in the variable and allocate the appropriate amount of memory for it.

    Rate this question:

  • 30. 

     Booleans are _______.

    • A.

       True or False

    • B.

       Single characters

    Correct Answer
    A.  True or False
    Explanation
    Booleans are data types that can only have two values: true or false. They are used to represent logical values and are commonly used in programming to make decisions or comparisons. The correct answer, "True or False," accurately describes the two possible values of booleans.

    Rate this question:

  • 31. 

    What is an assignment statement ?

    • A.

      Assigning a name to a variable

    • B.

      Assigning a value to a variable

    Correct Answer
    B. Assigning a value to a variable
    Explanation
    An assignment statement is a programming statement that assigns a value to a variable. It is used to store and update data in a variable by assigning a specific value to it. This allows the programmer to manipulate and work with the data stored in the variable throughout the program. By assigning a value to a variable, it can be used in calculations, comparisons, or other operations within the program.

    Rate this question:

  • 32. 

     If you want your conditional to depend on two conditions BOTH being true, what is the proper notation to put between the two Boolean statements ?

    • A.

      &&

    • B.

      !!

    Correct Answer
    A. &&
    Explanation
    The proper notation to put between two Boolean statements if you want your conditional to depend on both conditions being true is "&&". This is known as the logical AND operator in many programming languages. It returns true if both conditions are true, otherwise it returns false.

    Rate this question:

  • 33. 

    Which of the following means that in order for the conditional to happen, either x must be less than 3 or y must be greater than or equal to 4 ?

    • A.

       if ((x < 3) || (y > = 4))

    • B.

       if ((x > 3) || (y < = 4))

    Correct Answer
    A.  if ((x < 3) || (y > = 4))
    Explanation
    The correct answer is "if ((x < 3) || (y >= 4))". This means that for the conditional statement to be true, either x must be less than 3 or y must be greater than or equal to 4.

    Rate this question:

  • 34. 

     What is a loop ?

    • A.

       A new type of Applet

    • B.

       A segment of code to be run a specified amount of times

    Correct Answer
    B.  A segment of code to be run a specified amount of times
    Explanation
    A loop is a segment of code that is designed to be executed repeatedly for a specified number of times. It allows for the repetition of a certain set of instructions until a specific condition is met. Loops are commonly used in programming to automate repetitive tasks or to iterate over a collection of data. They help in improving code efficiency and reducing redundancy by eliminating the need for writing the same code multiple times.

    Rate this question:

  • 35. 

    What is essential in making sure that your loop is not infinite ?

    • A.

      That there is a Boolean statement somewhere in your code

    • B.

      That your Boolean statement will at some point be false

    Correct Answer
    B. That your Boolean statement will at some point be false
    Explanation
    To ensure that a loop is not infinite, it is essential to have a Boolean statement in the code that will eventually become false. This is because a loop continues to execute as long as the Boolean statement is true. If the statement never becomes false, the loop will continue indefinitely, resulting in an infinite loop. Therefore, having a Boolean statement that will eventually evaluate to false is necessary to terminate the loop and prevent it from running infinitely.

    Rate this question:

  • 36. 

    Which is NOT a section of all types of loops ?

    • A.

       Test statement

    • B.

       The word "while"

    Correct Answer
    B.  The word "while"
    Explanation
    The word "while" is not a section of all types of loops. While loops are a specific type of loop that have a test statement followed by a block of code. Other types of loops, such as for loops and do-while loops, do not necessarily have a test statement in the same format as a while loop. Therefore, the word "while" is not a section that is common to all types of loops.

    Rate this question:

  • 37. 

    In a ‘for’ loop, what section of the loop is not included in the parentheses after “for” ?

    • A.

       Loop Body

    • B.

       Initialization

    Correct Answer
    A.  Loop Body
    Explanation
    In a 'for' loop, the loop body is not included in the parentheses after 'for'. The loop body is the section of the code that is executed repeatedly until the loop condition is false. The initialization section, on the other hand, is included in the parentheses after 'for' and is responsible for initializing the loop control variable.

    Rate this question:

  • 38. 

    What is a function in terms of Computer Science ?

    • A.

       A group of code lines that performs a specific task

    • B.

       Something that contains an ‘init’

    Correct Answer
    A.  A group of code lines that performs a specific task
    Explanation
    A function in computer science refers to a group of code lines that are designed to perform a specific task. It is a modular approach that allows for code reusability and organization. Functions can be called and executed whenever needed, making the code more efficient and easier to maintain. By encapsulating a specific task within a function, it becomes easier to debug and modify the code, enhancing the overall functionality of the program.

    Rate this question:

  • 39. 

    What does AWT stands for ?

    • A.

       Abstract window Toolkit

    • B.

       Adjust Window Toolkit

    Correct Answer
    A.  Abstract window Toolkit
    Explanation
    AWT stands for Abstract Window Toolkit. It is a set of application programming interfaces (APIs) provided by Java for creating graphical user interfaces (GUIs) for Java programs. AWT provides a collection of classes and methods that allow developers to create and manage windows, buttons, menus, and other GUI components. It is a part of the Java Foundation Classes (JFC) and is platform-independent, meaning that GUIs created using AWT will look and behave the same on all platforms that support Java.

    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
  • Feb 09, 2024
    Quiz Edited by
    ProProfs Editorial Team
  • Jun 10, 2015
    Quiz Created by
    Volt02
Back to Top Back to top
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.