Java Quiz: Toughest Questions! Trivia MCQ

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 Kdjaafar
K
Kdjaafar
Community Contributor
Quizzes Created: 2 | Total Attempts: 1,619
| Attempts: 195
SettingsSettings
Please wait...
  • 1/93 Questions

    Given the declaration : int [ ] ar = {1,2,3,4,5}; What is the value of ar[4]?

    • 2
    • 3
    • 4
    • 5
Please wait...
About This Quiz

Challenge your Java knowledge with this trivia! Featuring tough questions on object-oriented programming, class accessibility, and object definitions, this quiz tests your understanding and skills in Java, enhancing your programming acumen.

Java Quiz: Toughest Questions! Trivia MCQ - Quiz

Quiz Preview

  • 2. 

    The range of indices for an array always start at:

    • Whatever programmer specifies

    • 1

    • 0

    • Size of array

    Correct Answer
    A. 0
    Explanation
    In programming, the range of indices for an array always starts at 0. This means that the first element in an array is accessed using the index 0, the second element with index 1, and so on. This convention is followed in many programming languages, including C, C++, Java, and Python. Starting the index at 0 allows for a more efficient and consistent way of referencing elements in an array.

    Rate this question:

  • 3. 

    The following prototype shows that a Cylinder subclass is derived from a superclass called Circle

    • Class Circle extends Cylinder

    • Class Cylinder derived Circle

    • Class Cylinder extends Circle

    • Class Circle derived Cylinder

    Correct Answer
    A. Class Cylinder extends Circle
    Explanation
    The correct answer is "class Cylinder extends Circle". This is because the keyword "extends" is used in Java to indicate that a class is derived from another class. In this case, the subclass "Cylinder" is derived from the superclass "Circle". This means that the "Cylinder" class inherits all the properties and methods of the "Circle" class, and can also have its own additional properties and methods.

    Rate this question:

  • 4. 

    Choose the best definition for a Class.    

    • A group of students in a class

    • An object definition, containing the data and function elements necessary to create an object

    • An action for a program

    Correct Answer
    A. An object definition, containing the data and function elements necessary to create an object
    Explanation
    A class is an object definition that contains the data and function elements necessary to create an object. It serves as a blueprint or template for creating objects of that class. The class defines the properties and behaviors that the objects of that class will have.

    Rate this question:

  • 5. 

    Choose the best definition of an object

    • A thing

    • An instance of a class

    • Something you wear

    Correct Answer
    A. An instance of a class
    Explanation
    The correct answer is "an instance of a class." In object-oriented programming, an object is an instance of a class, which is a blueprint for creating objects. Objects have properties (attributes) and behaviors (methods) defined by the class. This definition accurately describes the concept of an object in the context of programming.

    Rate this question:

  • 6. 

    Choose the appropriate data type for this value: true    

    • Int

    • Double

    • String

    • Boolean

    Correct Answer
    A. Boolean
    Explanation
    The value "true" represents a boolean data type, which is used to store either true or false values. In this case, since the value is "true", the appropriate data type to store it would be boolean.

    Rate this question:

  • 7. 

    Choose the appropriate data type for this value: 5.5    

    • Int

    • Double

    • Boolean

    • String

    Correct Answer
    A. Double
    Explanation
    The value 5.5 is a decimal number with a fractional component, which cannot be represented accurately using an integer data type. Therefore, the appropriate data type for this value is double, which is used to store floating-point numbers with a higher precision than float data type.

    Rate this question:

  • 8. 

    Choose the appropriate data type for this value: A    

    • Int

    • Double

    • Char

    • String

    Correct Answer
    A. Char
    Explanation
    The appropriate data type for the value "A" is char because char is used to represent a single character in programming. In this case, "A" is a single character, so it can be stored as a char data type.

    Rate this question:

  • 9. 

    Java runs on _______.

    • Windows

    • Unix/Linux

    • Mac

    • All of the Above

    Correct Answer
    A. 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, Unix/Linux, or Mac. Therefore, the correct answer is "All of the Above" as Java can run on all of these operating systems.

    Rate this question:

  • 10. 

    What is a loop?

    • A new type of Applet.

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

    • A segment of code to be run infinite times

    • A segment of code to be run once

    Correct Answer
    A. 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, which can be useful in situations where the same task needs to be performed multiple times. By specifying the number of times the loop should run, the code within the loop can be executed repeatedly until the specified condition is met, providing a way to automate repetitive tasks and streamline programming processes.

    Rate this question:

  • 11. 

    Choose the appropriate data type for this field: numberOfEggs    

    • Double

    • Char

    • Boolean

    • Int

    Correct Answer
    A. Int
    Explanation
    The appropriate data type for the field "numberOfEggs" would be int because it is most commonly used to represent whole numbers without any decimal places. Since the number of eggs is typically a whole number, using int would be the most suitable choice.

    Rate this question:

  • 12. 

    The last value in an array called ar can be found at index:

    • 0

    • 1

    • Ar.length

    • Ar.length - 1

    Correct Answer
    A. Ar.length - 1
    Explanation
    The last value in an array can be found at the index ar.length - 1 because arrays in most programming languages are zero-indexed, meaning the first element is at index 0. Therefore, the last element will be at the index equal to the length of the array minus one.

    Rate this question:

  • 13. 

    If classes Student, Staff and Faculty extend class Person, which one makes sense:

    • Faculty[] faculties={new Person(), new Staff(), new Student()};

    • Staff[] staff={new Person(), new Faculty(), new Student()};

    • Person[] persons={new Faculty(), new Staff(), new Student()};

    Correct Answer
    A. Person[] persons={new Faculty(), new Staff(), new Student()};
    Explanation
    The correct answer is "Person[] persons={new Faculty(), new Staff(), new Student()};". This is because the class hierarchy suggests that Faculty, Staff, and Student are all types of Person. Therefore, an array of type Person can hold objects of any of these three classes. However, the other two options are not valid because they involve trying to assign objects of incompatible types to arrays.

    Rate this question:

  • 14. 

    Choose the appropriate data type for this value: 1

    • Int

    • Double

    • String

    • Boolean

    Correct Answer
    A. Int
    Explanation
    The value 1 is a whole number and does not have any fractional or decimal part. Therefore, the appropriate data type for this value would be an integer (int). Integers are used to represent whole numbers in programming languages.

    Rate this question:

  • 15. 

    What is the role of the constructor? 

    • Create an instance of a class (an object)

    • Create names for methods

    • To create some type of change in the state of an object

    Correct Answer
    A. Create an instance of a class (an object)
    Explanation
    Constructors have one purpose in life: to create an instance of a class. This can also be called creating an object.

    Rate this question:

  • 16. 

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

    • True

    • False

    Correct Answer
    A. True
    Explanation
    The explanation for the given correct answer is that the "synchronized" keyword in Java is used to ensure that only one thread can access a particular block of code or method at a time. When a thread encounters a synchronized block, it grabs the lock associated with the object and executes the code inside the block. This ensures that other threads have to wait until the lock is released before they can access the same code. Therefore, the statement that "synchronized is a keyword to tell a Thread to grab an Object lock before continuing execution" is true.

    Rate this question:

  • 17. 

    What is an Applet?

    • A Java program that does not run through a web browser

    • A Java program that is run through a web browser

    • An object-oriented programming language

    • An interactive website

    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 embedded within an HTML page and is executed on the client-side. Applets are used to enhance the functionality of a web page by providing interactive features such as animations, games, and data visualization. They are typically smaller in size and have limited access to system resources compared to standalone Java applications.

    Rate this question:

  • 18. 

    An abstract class can have non-abstract methods.

    • True

    • 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 extended by other classes. It can contain both abstract and non-abstract methods. Non-abstract methods in an abstract class provide default implementations that can be inherited by its subclasses. This allows the subclasses to use these methods without having to implement them again.

    Rate this question:

  • 19. 

    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?

    • &

    • &&

    • |

    • ||

    Correct Answer
    A. &&
    Explanation
    The correct notation to put between the two Boolean statements if you want your conditional to depend on both conditions being true is "&&". This is the logical AND operator in many programming languages, which returns true only if both conditions are true.

    Rate this question:

  • 20. 

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

    • True

    • 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 in a superclass, it can be called and used in any subclass that inherits from that superclass. This allows for the subclass to have access to the protected method and utilize its functionality as needed. Therefore, the statement "Methods that are marked protected can be called in any subclass of that class" is true.

    Rate this question:

  • 21. 

    What is the difference between private and public functions?

    • Public functions are free, you have to buy private ones

    • Public functions are the only ones you can download

    • Public functions can be used by anyone, private can only be used by other code in the class you are writing

    • Public functions can’t be used

    Correct Answer
    A. Public functions can be used by anyone, private can only be used by other code in the class you are writing
    Explanation
    Public functions are accessible to anyone and can be used by any code, whether it is within the same class or outside of it. On the other hand, private functions can only be accessed and used by other code within the same class. They are not accessible or usable by any external code. This distinction allows for better encapsulation and control over the functionality of the class, as private functions can only be invoked and manipulated by the class itself.

    Rate this question:

  • 22. 

    Which is not a part of defining an object?

    • Description

    • Methods

    • Associations with other objects

    • Name

    Correct Answer
    A. Description
    Explanation
    In object-oriented programming, defining an object involves specifying its methods, associations with other objects, and giving it a name. However, the description of an object is not a necessary part of its definition. While it can be helpful to provide a description for documentation or understanding purposes, it is not essential for defining the object itself.

    Rate this question:

  • 23. 

    Given the declaration int [ ] nums = {8, 12, 23, 4, 15}, what expression will display the first element in the array (ie the number 8)

    • System.out.print("The number is : " + nums[0]);

    • System.out.print("The number is : " + nums[1]);

    • System.out.print("The number is : " + nums[8]);

    • System.out.print("The number is : " + nums);

    Correct Answer
    A. System.out.print("The number is : " + nums[0]);
    Explanation
    The correct answer is System.out.print("The number is : " + nums[0]); because the expression nums[0] accesses the first element in the array "nums" and concatenates it with the string "The number is : " to display the result.

    Rate this question:

  • 24. 

    What is the size of a Char in Java?

    • 4 bits

    • 7 bits

    • 8 bits

    • 16 bits

    Correct Answer
    A. 16 bits
    Explanation
    In Java, the size of a Char is 16 bits. This means that it occupies 2 bytes of memory. The Char data type is used to represent a single character and can store Unicode characters as well. The 16-bit size allows for a wide range of characters to be represented, including those from different languages and special symbols.

    Rate this question:

  • 25. 

    Can you compare a boolean to an integer?

    • Yes

    • No

    Correct Answer
    A. No
    Explanation
    A boolean and an integer are two different data types and cannot be directly compared. They represent different types of values - a boolean represents a logical value of either true or false, while an integer represents a numerical value. Therefore, it is not possible to compare a boolean to an integer.

    Rate this question:

  • 26. 

    What is the keyword used in java to create an object?

    • This

    • New

    • Sync

    • New()

    Correct Answer
    A. New
    Explanation
    The keyword used in Java to create an object is "new". This keyword is followed by the name of the class that we want to create an object of, and it allocates memory for the object and returns a reference to it.

    Rate this question:

  • 27. 

    What is an assignment statement?

    • Adding a number to an int

    • Assigning a multiplication

    • Assigning a name to a variable

    • Assigning a value to a variable

    Correct Answer
    A. Assigning a value to a variable
    Explanation
    An assignment statement is a statement in programming where a value is assigned to a variable. It is used to store a value in a variable so that it can be accessed and manipulated later in the program. In this case, assigning a value to a variable is the correct answer as it accurately describes the purpose of an assignment statement.

    Rate this question:

  • 28. 

    What will be the value of “num” after the following statements? int num; num = (5+4); num = num / 9; num = 9;

    • 9

    • 1

    • 0

    • Num

    Correct Answer
    A. 9
    Explanation
    The value of "num" will be 9 because the variable "num" is initially assigned the value of (5+4), which is 9. Then, the value of "num" is divided by 9, resulting in 1. However, the next statement assigns the value of 9 to "num", overwriting the previous value. Therefore, the final value of "num" is 9.

    Rate this question:

  • 29. 

    If we declare int [ ] ar = {1,2,3,4,5,6}; The size of array ar is:

    • 0

    • 5

    • 6

    • 7

    Correct Answer
    A. 6
    Explanation
    The size of the array "ar" is 6 because the given declaration "int [ ] ar = {1,2,3,4,5,6};" initializes an array with 6 elements. The numbers inside the curly braces represent the values of the elements in the array. Therefore, the size of the array is equal to the number of elements it contains, which in this case is 6.

    Rate this question:

  • 30. 

    What is the proper way to declare a variable?

    • VariableName variableType;

    • VariableName;

    • VariableType;

    • VariableType variableName;

    Correct Answer
    A. VariableType variableName;
    Explanation
    The proper way to declare a variable is by stating the variable type followed by the variable name. This ensures that the variable is properly defined and can be used in the program.

    Rate this question:

  • 31. 

    Choose the appropriate data type for this field: kindOfBird    

    • String

    • Int

    • Char

    • Double

    Correct Answer
    A. String
    Explanation
    example: kindOfBird = "Parrot"

    Rate this question:

  • 32. 

    With inheritance, a derived subclass object can directly access any

    • Public or private superclass member

    • Private superclass member

    • Public superclass member (and protected subclass members if it's in the same package)

    • Protected, public or private superclass member

    Correct Answer
    A. Public superclass member (and protected subclass members if it's in the same package)
    Explanation
    Inheritance allows a derived subclass object to directly access public superclass members. This means that the derived subclass object can use any public member of its superclass without any restrictions. Additionally, if the subclass is in the same package as the superclass, it can also access protected subclass members. However, private superclass members cannot be accessed directly by the subclass object.

    Rate this question:

  • 33. 

    Primitive datatypes are allocated on a stack.

    • True

    • False

    Correct Answer
    A. True
    Explanation
    Primitive data types in most programming languages are allocated on the stack. The stack is a region of memory that is used for local variables and function calls. When a primitive data type is declared, it is allocated on the stack and its value is stored directly in memory. This allows for fast and efficient access to the data. In contrast, objects and complex data types are typically allocated on the heap, which is a region of memory used for dynamic memory allocation.

    Rate this question:

  • 34. 

    What is the main function of any variable?

    • To add numbers together

    • To keep track of data in the memory of the computer

    • To print words on the screen

    • To write Java codes

    Correct Answer
    A. 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 provide a way to store values that can be accessed and modified throughout the program. By assigning values to variables, programmers can store and retrieve data as needed, making it easier to process and manipulate information in the computer's memory.

    Rate this question:

  • 35. 

    What does GUI stand for?

    • Graphical User Interface

    • Gimme Ur Internet

    • Grand User Interface

    • Graphical Useful Interface

    Correct Answer
    A. Graphical User Interface
    Explanation
    GUI stands for Graphical User Interface. It is a type of user interface that allows users to interact with electronic devices through graphical icons and visual indicators. This interface uses images, buttons, menus, and other graphical elements to enable users to perform tasks easily and intuitively. GUIs are widely used in software applications, operating systems, and websites to enhance user experience and make it more user-friendly.

    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?

    • If ((x < 3) && (y > 4))

    • If (x < 3 y >= 4)

    • If ((x < 3) || (y > = 4))

    • If ((x > 3) || (y < = 4))

    Correct Answer
    A. If ((x < 3) || (y > = 4))
    Explanation
    The correct answer is if ((x < 3) || (y >= 4)). This answer correctly represents the condition that either x must be less than 3 or y must be greater than or equal to 4. The logical operator "||" represents the logical OR, which means that if either of the conditions is true, the entire condition will be true. Therefore, this answer accurately captures the requirement stated in the question.

    Rate this question:

  • 37. 

    How to define a JButton with the caption test?

    • JButton aButton('test');

    • JButton aButton=new JButton("test");

    • JButton aButton=new JButton('test');

    • JButton("test") aButton;

    Correct Answer
    A. JButton aButton=new JButton("test");
    Explanation
    The correct answer is JButton aButton=new JButton("test"). This is the correct way to define a JButton with the caption "test". The JButton class provides a constructor that takes a String parameter, which represents the text that will be displayed on the button. By using the "new" keyword, we create a new instance of the JButton class and assign it to the variable "aButton". The String "test" is passed as an argument to the constructor, setting the caption of the button to "test".

    Rate this question:

  • 38. 

    A class is...

    • An object

    • An executable piece of code

    • An abstract definition of an object

    • A varibale

    Correct Answer
    A. An abstract definition of an object
    Explanation
    A class is an abstract definition of an object because it defines the properties and behaviors that an object of that class should have. It serves as a blueprint or template for creating objects with similar characteristics. A class does not represent a specific object itself, but rather describes the common attributes and methods that objects of that class should possess.

    Rate this question:

  • 39. 

    What would display from the following statements? int [ ] nums = {1,2,3,4,5,6}; System.out.println((nums[1] + nums[3]));

    • 6

    • 2+4

    • 1+3

    • 4

    Correct Answer
    A. 6
    Explanation
    The given code initializes an array called "nums" with the values {1,2,3,4,5,6}. The statement "System.out.println((nums[1] + nums[3]));" prints the sum of the values at index 1 and index 3 of the array, which are 2 and 4 respectively. Therefore, the output will be 6.

    Rate this question:

  • 40. 

    What displays from the following statements? String word = "abcde"; for (int i = 0; i <4; i+=2) System.out.print(word[i]);

    • Ab

    • Ac

    • Ace

    • Bd

    Correct Answer
    A. Ac
    Explanation
    The given code initializes a string variable "word" with the value "abcde". Then, it enters a for loop that starts with i=0 and increments i by 2 in each iteration until i

    Rate this question:

  • 41. 

    Choose the appropriate data type for this field: weightInKilos    

    • Char

    • String

    • Double

    • Boolean

    Correct Answer
    A. Double
    Explanation
    The appropriate data type for the field "weightInKilos" would be double. This is because weight is typically represented as a decimal number and the double data type can store decimal values with a higher precision compared to float. Using a double data type allows for more accurate and flexible calculations involving weight measurements.

    Rate this question:

  • 42. 

    Choose the appropriate data type for this field: isSwimmer    

    • Double

    • Boolean

    • String

    • Int

    Correct Answer
    A. Boolean
    Explanation
    isSwimmer - yes or no (only two possible answers = boolean)

    Rate this question:

  • 43. 

    Which is NOT a section of all types of loops?

    • Initialization

    • Loop Body

    • Test statement

    • The word "while"

    Correct Answer
    A. The word "while"
    Explanation
    The word "while" is not a section of all types of loops. It is specifically used in the syntax of the "while" loop, but not in other types of loops such as "for" or "do-while" loops. The "while" loop is a type of loop that continues to execute a block of code as long as a specified condition is true. Therefore, the word "while" is not a common section found in all types of loops.

    Rate this question:

  • 44. 

    Which one needs a web page to run?

    • A Java Application

    • A Java Stand-Alone Application

    • A Java Applet

    • A Java Class

    Correct Answer
    A. A Java Applet
    Explanation
    A Java Applet needs a web page to run because it is a small program that is designed to be embedded within a web page. It is executed within a web browser's Java Virtual Machine (JVM) and is typically used to add interactive features to a website. Unlike a Java Application or a Java Stand-Alone Application, which can be run independently on a computer, a Java Applet requires the infrastructure of a web page to be displayed and executed.

    Rate this question:

  • 45. 

    Given the declaration : int [ ] ar = {1,2,3,4,5}; What is the value of ar[3]?

    • 2

    • 3

    • 4

    • 5

    Correct Answer
    A. 4
    Explanation
    The value of ar[3] is 4 because in the given declaration, the array 'ar' is initialized with the values {1,2,3,4,5}. In an array, the index starts from 0, so ar[3] refers to the fourth element in the array, which is 4.

    Rate this question:

  • 46. 

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

    • True

    • 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 coordination. These methods are used to implement the wait-notify mechanism, where a thread can wait for a certain condition to be satisfied and then notify other threads that the condition has been met. In order to avoid race conditions and ensure thread safety, these methods should be called within synchronized blocks or methods, which provide exclusive access to the shared resources.

    Rate this question:

  • 47. 

    What loop will display each of the numbers in this array on a separate line: float [ ] nums= {1.1f, 2.2f, 3.3f};

    • For (int i =0; i < 3; i++) System.out.println( nums[i]);

    • For (i = 1; i

    • For (i = 0; i

    • For (i = 1; i < 3; i++) System.out.println(nums[i]);

    Correct Answer
    A. For (int i =0; i < 3; i++) System.out.println( nums[i]);
    Explanation
    The correct answer is "for (int i =0; i < 3; i++) System.out.println( nums[i]);". This loop iterates over the array "nums" using the index variable "i" starting from 0 and going up to 2. It then prints each element of the array on a separate line using the System.out.println() method.

    Rate this question:

  • 48. 

    What is an instanceof?

    • A methods in object

    • An operator and keyword

    • An operator only

    • A keyword only

    Correct Answer
    A. An operator and keyword
    Explanation
    The correct answer is "An operator and keyword." The instanceof operator is used to check whether an object belongs to a specific class or interface. It returns true if the object is an instance of the specified class or interface, and false otherwise. Therefore, it is both an operator and a keyword in Java.

    Rate this question:

  • 49. 

    Which of the following is not a Java keyword?

    • Main

    • Try

    • Integer

    • String

    Correct Answer
    A. Integer
    Explanation
    The correct answer is "integer" because "integer" is not a Java keyword. Java keywords are reserved words that have a specific meaning and purpose in the Java programming language. Some examples of Java keywords include "main" and "try", which are used for defining the main method and handling exceptions, respectively. "String" is also a Java keyword, used for defining string variables and manipulating strings. However, "integer" is not a Java keyword and cannot be used as a keyword in Java programming.

    Rate this question:

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

Our quizzes are rigorously reviewed, monitored and continuously updated by our expert board to maintain accuracy, relevance, and timeliness.

  • Current Version
  • Mar 18, 2023
    Quiz Edited by
    ProProfs Editorial Team
  • Aug 03, 2015
    Quiz Created by
    Kdjaafar
Back to Top Back to top
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.