Core Java Quiz And Java Online Test Quiz

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 Yfotto
Y
Yfotto
Community Contributor
Quizzes Created: 1 | Total Attempts: 267
| Attempts: 267 | Questions: 15
Please wait...
Question 1 / 15
0 %
0/100
Score 0/100
1. Is the following statement true or false? The constructor of a class must not have a return type.

Explanation

The statement is true. In object-oriented programming, a constructor is a special method used to initialize objects of a class. It is called automatically when an object is created. Unlike other methods, a constructor does not have a return type, not even void. Its purpose is to set the initial values of the object's attributes and prepare it for use.

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

This Core Java Quiz assesses knowledge of Java programming fundamentals, including constructors, data types, operators, and control structures. It is designed for learners to test their understanding of... see morecore Java concepts, enhancing their coding skills and preparation for software development roles. see less

2. Is the following statement true or false. As the toString method is defined in the Object class, System.out.println can be used to print any object.

Explanation

The statement is true because the toString method is a default method defined in the Object class, which is the root class for all Java classes. This method is responsible for converting an object to its string representation. Since all classes inherit from the Object class, they also have access to the toString method. Therefore, System.out.println can be used to print any object because it internally calls the toString method to obtain the string representation of the object.

Submit
3. What is the number of bytes used by Java primitive long?

Explanation

The correct answer is 8. In Java, the primitive data type "long" is a 64-bit signed two's complement integer. Since 1 byte is equal to 8 bits, a "long" variable occupies 8 bytes of memory. Therefore, the number of bytes used by a Java primitive long is 8.

Submit
4. Which of the following is true?

Explanation

The explanation for the given correct answer is that the && operator in Java is used for short-circuited logical AND. This means that if the first operand of the && operator evaluates to false, the second operand is not evaluated because the overall result will always be false. This behavior is useful in situations where the second operand may have side effects or be computationally expensive, as it allows for efficient evaluation of logical expressions.

Submit
5. Name the keyword that makes a variable belong to a class, rather than being defined for each instance of the class.

Explanation

The keyword "static" is used to make a variable belong to a class, rather than being defined for each instance of the class. When a variable is declared as static, it is shared among all instances of the class and can be accessed without creating an object of the class. This allows the variable to have a single value across all instances and can be useful for maintaining common data or for creating utility methods that do not require an instance of the class.

Submit
6. Which of these is a legal definition of a method named m assuming it throws IOException, and returns void? Also assume that the method does not take any arguments. 

Explanation

All of the above options are incorrect except for the answer "void m() throws IOException{}". The correct answer is a legal definition of a method named m assuming it throws IOException and returns void. The keyword "void" indicates that the method does not return any value. The method name is "m" and it does not take any arguments. The "throws IOException" statement indicates that the method can throw an IOException, which means it can encounter an input or output error.

Submit
7. Assuming the declaration below, which of the following statements would compile?      String s = new String("xyz"); 

Explanation

The statement "s = s + s;" would compile because it concatenates two strings together using the "+" operator. The resulting string is then assigned back to the variable "s".

Submit
8. What all gets printed when the following program is compiled and run?   public class test {         public static void main(String args[]) {                  int i=0, j=2;                 do {                         i=++i;                         j--;                 } while(j>0);                 System.out.println(i);         } }   

Explanation

The program will print 2. The do-while loop will execute once because the condition (j > 0) is initially true. Inside the loop, the value of i is incremented by 1 and the value of j is decremented by 1. After the first iteration, the value of i becomes 1 and the value of j becomes 1. Since the condition (j > 0) is false, the loop exits. Finally, the value of i (which is 1) is printed.

Submit
9. How can you ensure that the memory allocated by an object is freed?

Explanation

The correct answer is "Garbage collection cannot be forced. The programmer cannot force the JVM to free the memory used by an object." This is because garbage collection is managed by the JVM and it decides when to free the memory occupied by an object. The programmer can only suggest to the JVM that it is a good time to perform garbage collection by setting all references to the object to new values (null) or by calling the system.gc() method, but it does not guarantee that the memory will be immediately freed.

Submit
10. What gets printed when the following code is compiled and run?   public class test {      public static void main(String args[])      {                int i = 1;               do {                        i--;               } while (i > 2);               System.out.println(i);          } }    

Explanation

The code starts with the variable i initialized to 1. It then enters a do-while loop where i is decremented by 1. However, the condition for the loop to continue is that i is greater than 2, which is not the case since i is initially 1. Therefore, the loop is never executed and the value of i remains 1. After the loop, the value of i (which is still 1) is printed, resulting in the output 1.

Submit
11. What gets written on the screen when the following program is compiled and run?        public class test {              public static void main(String args[]) {                    int i;                   float  f = 2.3f;                   double d = 2.7;                   i = ((int)Math.ceil(f)) * ((int)Math.round(d));                    System.out.println(i);                  }      }  

Explanation

The program declares an integer variable i and assigns it the value of the result of multiplying the ceiling value of the float variable f with the rounded value of the double variable d. The ceiling function rounds up the float value to the nearest integer, while the round function rounds the double value to the nearest integer. Therefore, the value of i is 9.

Submit
12. What all gets printed when the following program is compiled and run? public class test {         public static void main(String args[]) {                  int i = 1;           switch(i) {                        case 0:                     System.out.println(0);                     break;                        case 1:                     System.out.println(1);                        case 2:                     System.out.println(2);                     break;                        case 3:                     System.out.println(3);                     break;                      }              }      }            

Explanation

The correct answer is "b, c" because when the program is run, the value of the variable "i" is 1. The switch statement checks the value of "i" and since it matches the case 1, it executes the code block under that case. Therefore, the output "1" is printed. However, there is no break statement after the code block for case 1, so the program continues to execute the code block for case 2 as well. As a result, the output "2" is also printed.

Submit
13. Given the following declarations, which of the assignments given in the options below would compile?             int i = 5;            boolean t = true;            float f = 2.3F;            double d = 2.3;    

Explanation

The assignment "d = i;" would compile because it is valid to assign an integer value to a double variable. The other assignments would not compile because they involve incompatible types. The assignment "t = (boolean) i;" would not compile because it is not possible to directly cast an integer to a boolean. The assignment "f = d;" would not compile because it is not possible to directly assign a double value to a float variable without a cast. The assignment "f = 2.8;" would not compile because it is not possible to assign a double literal to a float variable without a cast.

Submit
14. Which of these are legal array declarations or definitions?

Explanation

The correct answer is int[] []x[]; This is a legal array declaration because it declares a multidimensional array named x with unspecified dimensions. The int[] [] indicates that x is an array of arrays of integers. The additional [] after x indicates that x itself is also an array.

Submit
15. Assume that class A extends class B, which extends class C. Also all the three classes implement the method test(). How can a method in a class A invoke the test() method defined in class C (without creating a new instance of class C)?

Explanation

The correct answer is that it is not possible to invoke the test() method defined in class C from a method in class A. This is because the use of "super.super.test()" or "::test()" is not allowed in Java. The "super" keyword can only be used to invoke the immediate superclass's method, not a method from a superclass higher up in the inheritance hierarchy. Therefore, a method in class A cannot directly invoke the test() method defined in class C without creating a new instance of class C.

Submit
View My Results

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

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

  • Current Version
  • Mar 22, 2023
    Quiz Edited by
    ProProfs Editorial Team
  • May 29, 2012
    Quiz Created by
    Yfotto
Cancel
  • All
    All (15)
  • Unanswered
    Unanswered ()
  • Answered
    Answered ()
Is the following statement true or false? The constructor of a class...
Is the following statement true or false. As the toString method is...
What is the number of bytes used by Java primitive long?
Which of the following is true?
Name the keyword that makes a variable belong to a class, rather than...
Which of these is a legal definition of a method named m assuming it...
Assuming the declaration below, which of the following statements...
What all gets printed when the following program is compiled and run?...
How can you ensure that the memory allocated by an object is freed?
What gets printed when the following code is compiled and run?...
What gets written on the screen when the following program is compiled...
What all gets printed when the following program is compiled and run?...
Given the following declarations, which of the assignments given in...
Which of these are legal array declarations or definitions?
Assume that class A extends class B, which extends class C. Also all...
Alert!

Advertisement