Core Java Quiz And Java Online Test Quiz

Approved & Edited by ProProfs Editorial Team
At ProProfs Quizzes, our dedicated in-house team of experts takes pride in their work. With a sharp eye for detail, they meticulously review each quiz. This ensures that every quiz, taken by over 100 million users, meets our standards of accuracy, clarity, and engagement.
Learn about Our Editorial Process
| Written by Yfotto
Y
Yfotto
Community Contributor
Quizzes Created: 1 | Total Attempts: 240
Questions: 31 | Attempts: 240

SettingsSettingsSettings
Java Quizzes & Trivia

Questions and Answers
  • 1. 

    What feedback do you have for this quiz, if any?

  • 2. 

    How were you able to answer the previous question? Did you know the answer, take a guess, etc.?

  • 3. 

    How were you able to answer the previous question? Did you know the answer, take a guess, etc.?

  • 4. 

    How were you able to answer the previous question? Did you know the answer, take a guess, etc.?

  • 5. 

    How were you able to answer the previous question? Did you know the answer, take a guess, etc.?

  • 6. 

    How were you able to answer the previous question? Did you know the answer, take a guess, etc.?

  • 7. 

    How were you able to answer the previous question? Did you know the answer, take a guess, etc.?

  • 8. 

    How were you able to answer the previous question? Did you know the answer, take a guess, etc.?

  • 9. 

    How were you able to answer the previous question? Did you know the answer, take a guess, etc.?

  • 10. 

    How were you able to answer the previous question? Did you know the answer, take a guess, etc.?

  • 11. 

    How were you able to answer the previous question? Did you know the answer, take a guess, etc.?

  • 12. 

    How were you able to answer the previous question? Did you know the answer, take a guess, etc.?

  • 13. 

    How were you able to answer the previous question? Did you know the answer, take a guess, etc.?

  • 14. 

    How were you able to answer the previous question? Did you know the answer, take a guess, etc.?

  • 15. 

    How were you able to answer the previous question? Did you know the answer, take a guess, etc.?

  • 16. 

    How were you able to answer the previous question? Did you know the answer, take a guess, etc.?

  • 17. 

    Is the following statement true or false? The constructor of a class must not have a return type.

    • A. 

      True

    • B. 

      False

    Correct Answer
    A. True
    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.

    Rate this question:

  • 18. 

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

    • A. 

      The number of bytes is compiler dependent.

    • B. 

      2

    • C. 

      4

    • D. 

      8

    • E. 

      64

    • F. 

      None of the above

    Correct Answer
    D. 8
    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.

    Rate this question:

  • 19. 

    Which of the following is true?

    • A. 

      && operator is used for short-circuited logical AND.

    • B. 

      ~ operator is the bit-wise XOR operator.

    • C. 

      | operator is used to perform bitwise OR and also short-circuited logical OR.

    • D. 

      The unsigned right shift operator in Java is >>.

    Correct Answer
    A. && operator is used for short-circuited logical AND.
    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.

    Rate this question:

  • 20. 

    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);         } }   

    • A. 

      0

    • B. 

      1

    • C. 

      2

    • D. 

      The program does not compile.

    • E. 

      None of the above

    Correct Answer
    C. 2
    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.

    Rate this question:

  • 21. 

    Which of these are legal array declarations or definitions?

    • A. 

      Int[] []x[];

    • B. 

      Int *x;

    • C. 

      Int x[5];

    • D. 

      Int[] x = new int[];

    • E. 

      None of the above

    Correct Answer
    A. Int[] []x[];
    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.

    Rate this question:

  • 22. 

    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);          } }    

    • A. 

      0

    • B. 

      1

    • C. 

      2

    • D. 

      3

    • E. 

      None of the above

    Correct Answer
    A. 0
    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.

    Rate this question:

  • 23. 

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

    • A. 

      S = 2 * s;

    • B. 

      Int i = s[0];

    • C. 

      S = s + s;

    • D. 

      S = s >> 2;

    • E. 

      None of the above.

    Correct Answer
    C. S = s + s;
    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".

    Rate this question:

  • 24. 

    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)?

    • A. 

      Test();

    • B. 

      Super.test();

    • C. 

      Super.super.test();

    • D. 

      ::test();

    • E. 

      C.test();

    • F. 

      It is not possible to invoke test() method defined in C from a method in A.

    Correct Answer
    F. It is not possible to invoke test() method defined in C from a method in A.
    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.

    Rate this question:

  • 25. 

    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);                  }      }  

    • A. 

      4

    • B. 

      5

    • C. 

      6

    • D. 

      7

    • E. 

      9

    • F. 

      None of the above

    Correct Answer
    E. 9
    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.

    Rate this question:

  • 26. 

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

    • A. 

      Static

    • B. 

      Final

    • C. 

      Abstract

    • D. 

      Native

    • E. 

      Volatile

    • F. 

      Transient

    Correct Answer
    A. Static
    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.

    Rate this question:

  • 27. 

    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;    

    • A. 

      T = (boolean) i;

    • B. 

      F = d;

    • C. 

      D = i;

    • D. 

      F = 2.8;

    • E. 

      All of the above

    • F. 

      None of the above

    Correct Answer
    C. D = i;
    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.

    Rate this question:

  • 28. 

    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.

    • A. 

      True

    • B. 

      False

    Correct Answer
    A. True
    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.

    Rate this question:

  • 29. 

    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;                      }              }      }            

    • A. 

      0

    • B. 

      1

    • C. 

      2

    • D. 

      3

    • E. 

      A, b

    • F. 

      B, c

    • G. 

      C, d

    • H. 

      A, b, c, d

    • I. 

      None of the above

    Correct Answer
    F. B, c
    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.

    Rate this question:

  • 30. 

    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. 

    • A. 

      Void m() throws IOException{}

    • B. 

      Void m() throw IOException{}

    • C. 

      Void m(void) throws IOException{}

    • D. 

      M() throws IOException{}

    • E. 

      Void m() {} throws IOException

    • F. 

      All of the above

    • G. 

      None of the above

    Correct Answer
    A. Void m() throws IOException{}
    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.

    Rate this question:

  • 31. 

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

    • A. 

      By invoking the free method on the object.

    • B. 

      By calling system.gc() method.

    • C. 

      By setting all references to the object to new values (say null).

    • D. 

      Garbage collection cannot be forced. The programmer cannot force the JVM to free the memory used by an object.

    • E. 

      All of the above

    • F. 

      None of the above

    Correct Answer
    D. Garbage collection cannot be forced. The programmer cannot force the JVM to free the memory used by an object.
    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.

    Rate this question:

Back to Top Back to top
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.