Learn Functions Of Java Programming

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 Prachi Wadhwani
P
Prachi Wadhwani
Community Contributor
Quizzes Created: 2 | Total Attempts: 1,200
Questions: 42 | Attempts: 181

SettingsSettingsSettings
Java Quizzes & Trivia

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
    A char data type in most programming languages is typically represented by 8 bits, but in some cases, it can be represented by 16 bits. This allows it to store a wide range of characters, including special characters and non-English characters, which require more bits for representation. 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 restricts access to members within the same class only, not to the class itself. A class can be declared as public, protected, or default (no modifier). The public access modifier allows access to the class from any other class. The protected access modifier allows access to the class from its subclasses and classes in the same package. The default access modifier allows access to the class from classes in the same package only.

    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 given code will result in a compilation error because it is trying to assign a floating-point value (3.5) to an integer variable (a). In most programming languages, this is not allowed without explicitly converting the value to an integer using a type cast.

    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 simply casting an integer value to a float and assigning it to a double variable. Since a float can be safely cast to a double without any loss of precision, there will be no errors at runtime.

    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 given code will result in a runtime exception because it attempts to divide the number 9 by 0, which is not allowed in mathematics. This will throw an ArithmeticException at runtime.

    Rate this question:

  • 6. 

    Following code will result in: float a = 9/0

    • A.

      Compilation error: DivideByZeroException

    • B.

      Runtime Exception

    Correct Answer
    B. Runtime Exception
    Explanation
    The given code will result in a runtime exception because dividing any number by zero is not defined in mathematics. Therefore, when the code tries to divide 9 by 0, it will throw a runtime exception.

    Rate this question:

  • 7. 

    A class can be transient

    • A.

      True

    • B.

      False

    Correct Answer
    B. False
    Explanation
    A class cannot be transient. The transient keyword is used in Java to indicate that a variable should not be serialized when the class is being converted to a byte stream. It is used to exclude certain variables from the serialization process. However, the transient keyword cannot be applied to a class itself. It can only be used with variables within a class. Therefore, the statement that a class can be transient is false.

    Rate this question:

  • 8. 

    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 given code will result in a compilation error. This is because the variable "b" is an instance variable of class A, and it is being accessed in a static context (inside the main method) without creating an object of class A. To access the instance variable, it needs to be accessed through an object of the class.

    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.

      Compile error

    • B.

      No error

    Correct Answer
    A. Compile error
    Explanation
    The code will result in a compile error because class A is trying to create an object of class B, which is a subclass of A. However, this is not allowed because class B does not have a default constructor, and class A does not have a constructor that takes arguments. Therefore, the code cannot compile.

    Rate this question:

  • 10. 

    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 given code will not result in any errors because class B extends class A, so it is valid to assign an object of class B to a reference variable of class A. The code will compile and run without any issues.

    Rate this question:

  • 11. 

    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 if a method is marked as protected, it can be called and used in any subclass that extends the original class. This allows for the subclass to inherit and utilize the protected methods and their functionality. Therefore, the statement is true.

    Rate this question:

  • 12. 

    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 have both abstract and non-abstract methods. Non-abstract methods in an abstract class can have their own implementation and can be called directly from the class or its subclasses. This allows the abstract class to provide some default behavior that can be inherited and overridden by its subclasses.

    Rate this question:

  • 13. 

    Java keywords are written in lowercase as well as uppercase

    • A.

      True

    • B.

      False

    Correct Answer
    A. True
    Explanation
    Java keywords are written in lowercase as well as uppercase. In Java, keywords are predefined reserved words that have specific meanings and cannot be used as identifiers. These keywords are case-sensitive, meaning that they must be written in lowercase letters. However, Java also allows developers to write keywords in uppercase letters for better readability and code organization. Therefore, the statement that Java keywords are written in lowercase as well as uppercase is true.

    Rate this question:

  • 14. 

    What is an instanceof

    • A.

      A methods in object

    • B.

      An operator and keyword

    Correct Answer
    B. An operator and keyword
    Explanation
    "instanceof" is both an operator and a keyword in programming. It is used to check whether an object is an instance of a particular class or implements a specific interface. It returns a boolean value, true if the object is an instance of the class or implements the interface, and false otherwise. This operator is commonly used in object-oriented programming languages like Java.

    Rate this question:

  • 15. 

    Primitive datatypes are allocated on a stack

    • A.

      True

    • B.

      False

    Correct Answer
    A. True
    Explanation
    Primitive data types are allocated on a stack memory, which is a region of memory that is used for local variables and function calls. When a primitive data type is declared, it is stored directly in the stack memory. This allows for fast access and efficient memory management. In contrast, objects and reference types are allocated on the heap memory. The heap memory is used for dynamically allocated objects and is managed by the garbage collector.

    Rate this question:

  • 16. 

    Can you compare a boolean to an integer?

    • A.

      Yes

    • B.

      No

    Correct Answer
    B. No
    Explanation
    A boolean is a data type that can only have two possible values: true or false. An integer, on the other hand, is a data type that represents whole numbers. While it is possible to compare a boolean to an integer in some programming languages, it is not a common practice and can lead to unexpected results. In most cases, it is more appropriate to compare a boolean to another boolean or an integer to another integer.

    Rate this question:

  • 17. 

    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, if class A is declared as abstract, it can choose to not implement all the methods of the interface. Abstract classes are meant to be extended by other classes, so the responsibility of implementing the remaining methods can be delegated to the subclasses. Therefore, the correct answer is "No, not when A is abstract".

    Rate this question:

  • 18. 

    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
    When you use the "==" operator to compare two Integer objects, it checks if the two objects refer to the same memory location. In this case, although both a and b have the same value of 2, they are different objects because they were created using the "new" keyword. Therefore, the comparison "a==b" will return False.

    Rate this question:

  • 19. 

    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 these methods are used for inter-thread communication and coordination. When a thread calls wait(), it releases the lock it holds on the object and waits until another thread notifies it. Similarly, when a thread calls notify() or notifyAll(), it notifies the waiting threads to wake up and continue their execution. To ensure proper synchronization and avoid race conditions, these methods should be called within synchronized blocks or methods.

    Rate this question:

  • 20. 

    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 is useful when the inner class is only used within that method and does not need to be accessed from outside. Defining inner classes within methods can help with encapsulation and organization of code. It allows for more flexibility in designing and structuring classes within a program.

    Rate this question:

  • 21. 

    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 statement is true because the "synchronized" keyword in Java is used to ensure that only one thread can access a block of code or a method at a time. When a thread encounters a synchronized block or method, it tries to obtain the lock on the associated object before executing the code inside. If the lock is already held by another thread, the current thread waits until it can obtain the lock. This ensures that multiple threads do not interfere with each other's execution, preventing potential race conditions and ensuring thread safety.

    Rate this question:

  • 22. 

    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 being evaluated. If a matching case is found, the corresponding block of code is executed and the switch statement is exited. Therefore, the default statement will only be executed if none of the cases match the value.

    Rate this question:

  • 23. 

    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 prevents it from being serialized. Serialization is the process of converting an object into a stream of bytes to store it in memory, send it over a network, or save it to a file. When a variable is marked as transient, it tells the serialization mechanism to exclude that variable from the serialization process. This is useful when there are certain variables that should not be saved or transmitted along with the object, such as sensitive data or variables that are not serializable.

    Rate this question:

  • 24. 

    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 widely used in computer science. It is known for its platform independence, meaning that Java programs can run on any device or operating system that has a Java Virtual Machine (JVM). Java's object-oriented nature allows for the creation of modular and reusable code, making it easier to develop complex software systems. It also provides features like automatic memory management and exception handling, enhancing the reliability and robustness of Java programs. Overall, Java is a fundamental language in computer science that is used for various applications ranging from web development to mobile app development.

    Rate this question:

  • 25. 

    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 run within a web browser. It is a small application that is embedded within a webpage and can be executed by the Java Virtual Machine (JVM) installed on the user's computer. Applets are used to enhance the functionality of a webpage by providing interactive features and dynamic content. They can be used for various purposes such as animations, games, data visualization, and more.

    Rate this question:

  • 26. 

    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 limited 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:

  • 27. 

    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." This is because all three statements provided in the options are valid reasons why the whole program cannot just consist of the one line that does the painting. In Java, to create an applet, you need to define a class to call functions. Additionally, the drawString function is not defined without the "import" statements at the top. Therefore, all three statements contribute to the explanation of why the whole program cannot be just the one line of painting code.

    Rate this question:

  • 28. 

    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 embedded within web pages and run within a web browser. They are typically used to add interactive features to websites. 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 perform a wide range of tasks. Therefore, the correct answer is that applets are run over the web, distinguishing them from applications.

    Rate this question:

  • 29. 

    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 programmers to store values that can be accessed and modified throughout the program. By assigning a value to a variable, it can be stored in the computer's memory and used for calculations or other operations.

    Rate this question:

  • 30. 

    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 followed by the variable name. This allows the compiler or interpreter to allocate the appropriate amount of memory for the variable and to enforce type checking.

    Rate this question:

  • 31. 

     Booleans are _______.

    • A.

       True or False

    • B.

       Single characters

    Correct Answer
    A.  True or False
    Explanation
    Booleans are a data type that can have two possible values: true or false. They are often used in programming to represent logical values and make decisions based on conditions.

    Rate this question:

  • 32. 

    The following statements make “length” be what number ?

    • A.

       5

    • B.

      6

    Correct Answer
    A.  5
    Explanation
    The question is asking for the number that makes "length" be equal to it. The only number given in the statements is 5, so the correct answer is 5.

    Rate this question:

  • 33. 

    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 refers to the process of assigning a value to a variable. This means that a specific value is being assigned or stored in a variable for later use in a program. By assigning a value to a variable, we can manipulate and work with that value throughout the program.

    Rate this question:

  • 34. 

    What will be the value of “num” after the following statements?

    • A.

      1

    • B.

      12

    Correct Answer
    B. 12
    Explanation
    The value of "num" will be 12 because the given statements are 1 and 12, which means that the value of "num" is assigned to be 12 in the second statement.

    Rate this question:

  • 35. 

     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 when you want your conditional to depend on both conditions being true is "&&". This is the logical AND operator in many programming languages, including Java and C++. It returns true if both conditions are true, and false otherwise.

    Rate this question:

  • 36. 

    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 the conditional statement will be true if either x is less than 3 or y is greater than or equal to 4. It allows for either condition to be met in order for the statement to be true.

    Rate this question:

  • 37. 

     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 efficient repetition of a certain set of instructions, making it easier to perform repetitive tasks or iterate through a collection of data. By using loops, developers can save time and effort by automating the execution of code and avoid writing the same code multiple times.

    Rate this question:

  • 38. 

    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
    In order to ensure that a loop is not infinite, it is essential to have a Boolean statement somewhere in the code that will eventually become false. This is because a loop continues to execute as long as the Boolean statement remains 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 become false is necessary to control the execution of the loop and prevent it from running infinitely.

    Rate this question:

  • 39. 

    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 have a test statement that is evaluated before each iteration, and if the test statement is true, the loop continues to execute. However, other types of loops like for loops and do-while loops do not have a separate test statement section. Therefore, the word "while" is not a section that is common to all types of loops.

    Rate this question:

  • 40. 

    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. It is enclosed in curly braces ({}) and contains the statements that need to be executed in each iteration of the loop. The initialization section, on the other hand, is included in the parentheses after 'for' and is used to initialize the loop counter or any other variables that are needed for the loop.

    Rate this question:

  • 41. 

    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 is a group of code lines that performs a specific task. It is a way to organize and reuse code by encapsulating a set of instructions that can be called and executed multiple times throughout a program. Functions help in modularizing code, improving code readability, and promoting code reusability. They allow programmers to break down complex problems into smaller, manageable tasks, making it easier to understand and maintain the code.

    Rate this question:

  • 42. 

    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 Java libraries that provides a platform-independent way to create graphical user interfaces (GUIs) for Java programs. AWT includes components such as buttons, checkboxes, and text fields, as well as layout managers for arranging these components on a window. The AWT library is part of the Java Foundation Classes (JFC), which also includes the Swing library for creating more advanced GUIs.

    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 14, 2023
    Quiz Edited by
    ProProfs Editorial Team
  • May 18, 2017
    Quiz Created by
    Prachi Wadhwani
Back to Top Back to top
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.