Java Junior

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 Cvbee
C
Cvbee
Community Contributor
Quizzes Created: 1 | Total Attempts: 10,069
| Attempts: 10,088
SettingsSettings
Please wait...
  • 1/59 Questions

    Java runs on _______.

    • Windows
    • Unix/Linux
    • Mac
    • All of the Above
Please wait...
About This Quiz

It is created for Beginners in Java. This is to test the knowledge of any Java Developer wannabe.

Java Junior - Quiz

Quiz Preview

  • 2. 

    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 can be accessed and used by anyone, regardless of where they are defined. On the other hand, private functions can only be accessed and used by other code within the same class they are defined in. This means that private functions are restricted to be used only within the context of the class they belong to, while public functions can be used by any code that has access to them.

    Rate this question:

  • 3. 

    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 specifying the variable type followed by the variable name. This allows the compiler to allocate the appropriate amount of memory for the variable and ensures that the variable can be used correctly in the program.

    Rate this question:

  • 4. 

    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 variable "num" is divided by 9, resulting in 1. However, the value of "num" is reassigned to 9 in the next statement. Therefore, the final value of "num" is 9.

    Rate this question:

  • 5. 

    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:

  • 6. 

    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 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 programming. It returns true if both conditions are true and false otherwise.

    Rate this question:

  • 7. 

    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 in computer programs. They allow us to assign values to data and retrieve or modify those values as needed. By storing data in variables, we can access and manipulate it throughout the program, making it an essential component of computer programming.

    Rate this question:

  • 8. 

    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-based, meaning the first element is at index 0. Therefore, the last element will be at the index one less than the length of the array.

    Rate this question:

  • 9. 

    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 the first option, "for (int i =0; i < 3; i++) System.out.println( nums[i]);". This loop uses a for loop to iterate through the array "nums" and print each element on a separate line. The loop starts at index 0 and continues until the index is less than 3, incrementing the index by 1 each time. The "System.out.println(nums[i])" statement prints the element at the current index "i" in the array.

    Rate this question:

  • 10. 

    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 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. The "||" operator represents the logical OR, so if either of the conditions is true, the whole expression will evaluate to true.

    Rate this question:

  • 11. 

    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 the programmer to efficiently perform repetitive tasks without having to write the same code multiple times. By specifying the number of times the code should be executed, the loop ensures that the desired operations are carried out efficiently and accurately.

    Rate this question:

  • 12. 

    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 application that is designed to be embedded within a web page. It is executed within a web browser's Java Virtual Machine (JVM) and requires the web page's HTML code to provide the necessary environment for its execution. Unlike Java Applications or Stand-Alone Applications, which can be run independently on a computer, a Java Applet relies on the web page infrastructure to function properly.

    Rate this question:

  • 13. 

    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 programming construct that assigns a value to a variable. It is used to store and update data in a program. In this context, assigning a value to a variable means giving a specific value to a variable so that it can be used later in the program. This is a fundamental concept in programming as it allows for the manipulation and storage of data.

    Rate this question:

  • 14. 

    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 block of code or a method at a time. It provides a way to synchronize the execution of multiple threads, preventing them from interfering with each other. When a thread encounters a synchronized block or method, it grabs the lock on the associated object and continues execution. This ensures that other threads have to wait until the lock is released before they can access the synchronized 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:

  • 15. 

    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 to indicate that the class Cylinder is derived from the superclass Circle. In object-oriented programming, inheritance allows a subclass to inherit the properties and methods of its superclass, and in this case, the subclass Cylinder is inheriting from the superclass Circle.

    Rate this question:

  • 16. 

    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". In Java, when creating an object, the syntax is "new ClassName()", and in this case, the class name is JButton. The caption "test" is passed as a parameter within double quotes.

    Rate this question:

  • 17. 

    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 and called by any subclass of that class. This access modifier allows subclasses to inherit and use the protected methods from the parent class. It provides a level of accessibility that is more restricted than public, but less restricted than private. Therefore, the statement that protected methods can be called in any subclass of the class is true.

    Rate this question:

  • 18. 

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

    • This

    • New

    • Sync

    • New()

    Correct Answer
    A. New
    Explanation
    In Java, the keyword "new" is used to create an object. It is followed by the name of the class and parentheses, which can optionally contain arguments to be passed to the constructor of the class. The "new" keyword dynamically allocates memory for the object and initializes its state.

    Rate this question:

  • 19. 

    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 run within a web browser. It is a small application that is embedded within a web page and can be executed by a Java Virtual Machine (JVM) installed on the client's machine. Applets are used to add interactive features to websites and provide dynamic content. They have access to certain resources on the client's machine, such as the file system, but are restricted from performing certain actions for security reasons.

    Rate this question:

  • 20. 

    Primitive datatypes are allocated on a stack.

    • True

    • False

    Correct Answer
    A. True
    Explanation
    Primitive datatypes are allocated on a stack because they are simple and small in size. The stack is a region of memory that is used for local variables and function calls. When a primitive datatype is declared, memory is allocated on the stack to store its value. The stack is a fast and efficient way to allocate and deallocate memory for these types of variables.

    Rate this question:

  • 21. 

    Can you compare a boolean to an integer?

    • Yes

    • No

    Correct Answer
    A. 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 numeric data type that represents whole numbers. Since they are different data types, they cannot be directly compared.

    Rate this question:

  • 22. 

    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 code initializes an array called "nums" with the values {1,2,3,4,5,6}. It then 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:

  • 23. 

    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
    Additionally, a subclass can access protected members of the superclass regardless of the package if it is derived from the superclass. However, private superclass members are not directly accessible to the subclass.

    Rate this question:

  • 24. 

    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 synchronization. When a thread calls the wait() method, it releases the lock on the object and waits for another thread to notify it. Similarly, when a thread calls the notify() or notifyAll() method, it notifies the waiting threads that they can resume execution. To ensure thread safety and prevent race conditions, these methods should only be called from synchronized blocks or methods.

    Rate this question:

  • 25. 

    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 named "ar" with 6 elements. Each element is assigned a value from 1 to 6. Therefore, the size of the array is equal to the number of elements in it, which is 6.

    Rate this question:

  • 26. 

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

    • None of the above.

    • Applets are run over the web.

    • Applets can paint words, applications cannot.

    • An application is only available on Windows

    Correct Answer
    A. Applets are run over the web.
    Explanation
    An applet is a small program that is designed to be executed within a web browser. It is typically written in Java and runs on the client-side. On the other hand, an application is a standalone program that is designed to be executed on a computer's operating system. It is not limited to running within a web browser. Therefore, the difference between an applet and an application is that applets are specifically designed to be run over the web, while applications can be run on any operating system.

    Rate this question:

  • 27. 

    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 inherited by other classes. It can contain both abstract and non-abstract methods. Non-abstract methods in an abstract class have a complete implementation and can be directly used by the subclasses that inherit from the abstract class. This allows the abstract class to provide common functionality to its subclasses while still allowing them to implement their own specific behavior.

    Rate this question:

  • 28. 

    Java keywords are written in lowercase as well as uppercase.

    • True

    • False

    Correct Answer
    A. False
    Explanation
    Java keywords are written in lowercase only. In Java, keywords are predefined reserved words that have specific meanings and cannot be used as identifiers for variables, methods, or classes. These keywords are case-sensitive, meaning that they must be written in lowercase letters. Writing keywords in uppercase would result in a compilation error. Therefore, the statement that Java keywords are written in lowercase as well as uppercase is incorrect.

    Rate this question:

  • 29. 

    Class B inherits from Class A, what cannot be said:

    • B is a sub-class of A

    • A is a super-class of B

    • B has access to private members of A

    • B has access to protected members of A

    Correct Answer
    A. B has access to private members of A
    Explanation
    Since Class B inherits from Class A, it can access the protected and public members of Class A. However, it cannot access the private members of Class A. Private members are only accessible within the same class and not by any derived classes. Therefore, it cannot be said that B has access to the private members of A.

    Rate this question:

  • 30. 

    The default statement of a switch is always executed.

    • True

    • False

    Correct Answer
    A. 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 case matches, the corresponding block of code is executed and the switch statement terminates. If there is no default statement, and none of the cases match, the switch statement simply does nothing. Therefore, the correct answer is false.

    Rate this question:

  • 31. 

    Inner classes can be defined within methods.

    • True

    • False

    Correct Answer
    A. True
    Explanation
    Inner classes can be defined within methods. This statement is true. In Java, it is possible to define a class inside another class, and this inner class can also be defined within a method. This allows for more flexibility in organizing and encapsulating code. Inner classes have access to the variables and methods of the outer class, and can be useful in implementing complex logic or creating specialized data structures within a specific method scope.

    Rate this question:

  • 32. 

    What is a member of a class

    • An attribute

    • A method

    • Attribute or method

    • A sub-class

    Correct Answer
    A. Attribute or method
    Explanation
    A member of a class refers to any attribute or method that belongs to that class. It can be either an attribute, which represents the data or state of the object, or a method, which represents the behavior or actions that the object can perform. In object-oriented programming, classes are used to define the blueprint for creating objects, and the members of a class define the characteristics and capabilities of those objects. Therefore, a member of a class can be either an attribute or a method.

    Rate this question:

  • 33. 

    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 serves as a blueprint or template for creating specific instances of that object. It defines the properties and behaviors that the object will have, but it is not the actual object itself. The class provides a way to organize and structure the code, allowing for code reusability and modularity.

    Rate this question:

  • 34. 

    A null reference may be used to access a static variable or method.

    • True

    • False

    Correct Answer
    A. False
    Explanation
    A null reference cannot be used to access a static variable or method because static members belong to the class itself and not to any specific instance of the class. They can be accessed using the class name directly, without needing an instance of the class. A null reference represents the absence of an object and cannot be used to access any members, including static members. Therefore, the statement is false.

    Rate this question:

  • 35. 

    Following code will result in: int num = 6.7;

    • Compilation error

    • Runtime error

    • Num being 6.7

    • Num being 6

    Correct Answer
    A. Compilation error
    Explanation
    The given code will result in a compilation error. This is because the variable "num" is declared as an integer, but it is being assigned a floating-point value (6.7). In Java, you cannot assign a floating-point value to an integer variable without explicit type casting.

    Rate this question:

  • 36. 

    Following code will result in: float num = 5/0;

    • Compilation error: Divisions must be in a try block

    • Compilation error: DivideByZeroException

    • Runtime Exception

    • No Error: a is NaN

    Correct Answer
    A. Runtime Exception
    Explanation
    The code will result in a runtime exception because dividing a number by zero is not a valid operation in mathematics. This will throw an exception at runtime and the program will terminate abruptly.

    Rate this question:

  • 37. 

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

    • Compilation error

    • Runtime error

    • No errors

    • Runtime Exception

    Correct Answer
    A. No errors
    Explanation
    The given code will not result in any errors. It initializes an integer variable "a1" with the value 5 and then assigns the value of "a1" to a double variable "a2" after casting it to a float. Since both int and float can be implicitly converted to double without any loss of precision, there will be no compilation or runtime errors.

    Rate this question:

  • 38. 

    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:

  • 39. 

    If none of the private/protected/public is specified for a member, that member ...

    • Is accessible publicly

    • Is only accessible by other classes of the same package

    • Is only accessible from within the class

    • Is accessible by the class and its subclasses

    Correct Answer
    A. Is only accessible by other classes of the same package
    Explanation
    If none of the access modifiers (private, protected, public) is specified for a member, that member is only accessible by other classes of the same package. This means that the member can be accessed by any other class within the same package, but not by classes in different packages.

    Rate this question:

  • 40. 

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

    • Initialization

    • Loop Body

    • Test statement

    • Update

    Correct Answer
    A. Loop Body
    Explanation
    In a 'for' loop, the section of the loop that is not included in the parentheses after 'for' is the Loop Body. The loop body contains the code that is executed repeatedly until the loop condition becomes false. The initialization, test statement, and update sections are all included within the parentheses after 'for' and are responsible for controlling the loop's execution. The loop body is the block of code that is executed during each iteration of the loop.

    Rate this question:

  • 41. 

    Which is not a part of defining an object?

    • Description

    • Methods

    • Associations with other objects

    • Name

    Correct Answer
    A. Description
    Explanation
    An object in programming is typically defined by its name, methods, and associations with other objects. The description, on the other hand, is not a necessary component for defining an object. While a description can provide additional information about the object, it is not essential for its definition. The name is used to identify the object, methods define its behavior or actions, and associations establish relationships with other objects.

    Rate this question:

  • 42. 

    Which one is not correct?

    • A class needs to be instantiated as an object before being used

    • An objects exists in memory in run time

    • Class and object are just different names for the same thing

    • An object is a variable, where its type is the class used to declare the variable

    Correct Answer
    A. Class and object are just different names for the same thing
    Explanation
    The statement "Class and object are just different names for the same thing" is not correct. In object-oriented programming, a class is a blueprint or template for creating objects, while an object is an instance of a class. A class defines the properties and behaviors that an object will have, and multiple objects can be created from the same class. Therefore, a class and an object are not the same thing, but rather, a class is used to create objects.

    Rate this question:

  • 43. 

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

    • Compiler error

    • Runtime Exception

    • No errors

    • Output is B

    Correct Answer
    A. No errors
    Explanation
    The code will not result in any errors because class B extends class A, so it is valid to assign an instance of B to a variable of type A. The code will compile successfully and run without any issues.

    Rate this question:

  • 44. 

    A class can be transient

    • True

    • False

    Correct Answer
    A. False
    Explanation
    A class cannot be transient. The "transient" keyword in programming is used to indicate that a variable should not be serialized when an object is being converted to a byte stream. However, it cannot be applied to a class itself. The "transient" keyword can only be used with variables within a class to specify that they should not be included in the serialization process. Therefore, the statement that a class can be transient is incorrect.

    Rate this question:

  • 45. 

    How can you prevent a member variable from becoming serialized?

    • By marking it private

    • By marking it volatile

    • By marking it transient

    • You can not.

    Correct Answer
    A. By marking it transient
    Explanation
    A transient variable is a variable that may not be serialized.

    Rate this question:

  • 46. 

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

    • That your Boolean statement will at some point be false

    • That there is a Boolean statement somewhere in your code

    • That your Boolean statement will at some point be true

    • All of the choices

    Correct Answer
    A. That your Boolean statement will at some point be false
    Explanation
    To ensure that a loop is not infinite, it is essential that the Boolean statement used as the loop condition will eventually evaluate to false. This allows the loop to terminate and the program to move on to the next set of instructions. Without a false condition, the loop would continue indefinitely, causing the program to hang or crash. Therefore, it is crucial to have a Boolean statement that will eventually be false in order to prevent an infinite loop.

    Rate this question:

  • 47. 

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

    • Compiler error

    • Runtime Exception

    • True

    • False

    Correct Answer
    A. False
    Explanation
    When comparing the objects "a" and "b" using the "==" operator, it checks if the two objects refer to the same memory location. In this case, even though the values of "a" and "b" are both 2, they are different objects with different memory locations. Therefore, the condition "a==b" evaluates to false.

    Rate this question:

  • 48. 

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

    • Compile error

    • Runtime Exception

    • No error

    • No Output

    Correct Answer
    A. Compile error
    Explanation
    The given code will result in a compile error because the object 'b' is being declared as type 'B' but it is being assigned a new instance of class 'A'. Since 'A' is not a subclass of 'B', this assignment is not valid and will cause a compile error.

    Rate this question:

  • 49. 

    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 the for loop or the do-while loop. The other sections mentioned - Initialization, Loop Body, and Test statement - are common components found in all types of loops.

    Rate this question:

Quiz Review Timeline (Updated): Jun 16, 2024 +

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

  • Current Version
  • Jun 16, 2024
    Quiz Edited by
    ProProfs Editorial Team
  • Nov 11, 2013
    Quiz Created by
    Cvbee
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.