Java Fundamentals Test

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 311570
3
311570
Community Contributor
Quizzes Created: 3 | Total Attempts: 4,645
Questions: 30 | Attempts: 4,162

SettingsSettingsSettings
Java Fundamentals Test - Quiz

Hi


Questions and Answers
  • 1. 

    Choose the operations that can be performed on String objects: (A) + (B) + = (C) - (D) % (E) ^

    • A.

      (A)

    • B.

      (D)

    • C.

      (A) and (B)

    • D.

      (D) and (E)

    • E.

      None of the above

    Correct Answer
    C. (A) and (B)
    Explanation
    The "+" and "+ =" operators are overlaoded in Java. They join the two operands into one String object.

    Rate this question:

  • 2. 

    Which three are valid declarations of a float? 1.    float f1 = -343; 2.    float f2 = 3.14; 3.    float f3 = 0x12345; 4.    float f4 = 42e7; 5.    float f5 = 2001.0D; float f6 = 2.81F;

    • A.

      1,2,4

    • B.

      1,3,6

    • C.

      2,3,4

    • D.

      2,5,6

    Correct Answer
    B. 1,3,6
    Explanation
    (1) and (3) are integer literals (32 bits), and integers can be legally assigned tofloats (also 32 bits). (6) is correct because (F) is appended to the literal, declaring it as a float rather than a double (the default for floating point literals).
    (2), (4),and (5) are all doubles

    Rate this question:

  • 3. 

    What is the numerical range of a char?  

    • A.

      -128 to 127

    • B.

      -232 to -231

    • C.

      0 to 32767

    • D.

      0 to 65535

    Correct Answer
    D. 0 to 65535
    Explanation
    A char is really a 16-bit integer behind the scenes, so it supports 216 (from 0 to 65535) values.

    Rate this question:

  • 4. 

    Which one of these lists contains only Java programming language keywords?

    • A.

      Class, if, void, long, Int, continue

    • B.

      Goto, instanceof, native, finally, default, throws

    • C.

      Try, virtual, throw, final, volatile, transient

    • D.

      Strictfp, constant, super, implements, do

    • E.

      Byte, break, assert, switch, include

    Correct Answer
    B. Goto, instanceof, native, finally, default, throws
    Explanation
    All the words in option B are among the 49 Java keywords. Although goto reserved as a keyword in Java, goto is not used and has no function.

    Option A is wrong because the keyword for the primitive int starts with a lowercase i.

    Option C is wrong because "virtual" is a keyword in C++, but not Java.

    Option D is wrong because "constant" is not a keyword. Constants in Java are marked static and final.

    Option E is wrong because "include" is a keyword in C, but not in Java.

    Rate this question:

  • 5. 

    Which is a valid keyword in java?

    • A.

      Interface

    • B.

      String

    • C.

      Float

    • D.

      Unsigned

    Correct Answer
    A. Interface
    Explanation
    interface is a valid keyword.

    Option B is wrong because although "String" is a class type in Java, "string" is not a keyword.

    Option C is wrong because "Float" is a class type. The keyword for the Java primitive is float.

    Option D is wrong because "unsigned" is a keyword in C/C++ but not in Java

    Rate this question:

  • 6. 

    Which is a reserved word in the Java programming language?

    • A.

      Method

    • B.

      Native

    • C.

      Subclasses

    • D.

      Reference

    • E.

      Array

    Correct Answer
    B. Native
    Explanation
    The word "native" is a valid keyword, used to modify a method declaration.

    Option A, D and E are not keywords. Option C is wrong because the keyword for subclassing in Java is extends, not 'subclasses'.

    Rate this question:

  • 7. 

    Which three are valid declarations of a char?

    • A.

      Char c1 = 064770;

    • B.

      Char c2 = 'face';

    • C.

      Char c3 = 0xbeef;

    • D.

      Char c4 = \u0022;

    • E.

      Char c5 = '\iface';

    • F.

      Char c6 = '\uface';

    Correct Answer(s)
    A. Char c1 = 064770;
    C. Char c3 = 0xbeef;
    F. Char c6 = '\uface';
    Explanation
    (1), (3), and (6) are correct. char c1 = 064770; is an octal representation of the integer value 27128, which is legal because it fits into an unsigned 16-bit integer. char c3 = 0xbeef; is a hexadecimal representation of the integer value 48879, which fits into an unsigned 16-bit integer. char c6 = '\uface'; is a Unicode representation of a character.

    char c2 = 'face'; is wrong because you can't put more than one character in a char literal. The only other acceptable char literal that can go between single quotes is a Unicode value, and Unicode literals must always start with a '\u'.

    char c4 = \u0022; is wrong because the single quotes are missing.

    char c5 = '\iface'; is wrong because it appears to be a Unicode representation (notice the backslash), but starts with '\i' rather than '\u'.

    Rate this question:

  • 8. 

    What will be the output of the program? class BitShift { public static void main(String [] args) { int x = 0x80000000; System.out.print(x + " and "); x = x >>> 31; System.out.println(x); } }

    • A.

      -2147483648 and 1

    • B.

      0x80000000 and 0x00000001

    • C.

      -2147483648 and -1

    • D.

      1 and -2147483648

    Correct Answer
    A. -2147483648 and 1
    Explanation
    Option A is correct. The >>> operator moves all bits to the right, zero filling the left bits. The bit transformation looks like this:

    Before: 1000 0000 0000 0000 0000 0000 0000 0000

    After: 0000 0000 0000 0000 0000 0000 0000 0001

    Option C is incorrect because the >>> operator zero fills the left bits, which in this case changes the sign of x, as shown.

    Option B is incorrect because the output method print() always displays integers in base 10.

    Option D is incorrect because this is the reverse order of the two output numbers.

    Rate this question:

  • 9. 

    String is a primitive datatype

    • A.

      True

    • B.

      False

    Correct Answer
    B. False
    Explanation
    String is an object, it isn't a primitive type at all, just an array of chars.

    Rate this question:

  • 10. 

    The operations y >> 3 and y >>> 3 produce the same result when y > 0.   

    • A.

      True

    • B.

      False

    Correct Answer
    A. True
    Explanation
    The shift operation "y1 >>> y2" is identical to "y1 >> y2" for all positive values of y1. It shifts the bits in y1 to the right by y2 positions

    Rate this question:

  • 11. 

      What will be the output of the program? class SSBool { public static void main(String [] args) { boolean b1 = true; boolean b2 = false; boolean b3 = true; if ( b1 & b2 | b2 & b3 | b2 ) /* Line 8 */ System.out.print("ok "); if ( b1 & b2 | b2 & b3 | b2 | b1 ) /*Line 10*/ System.out.println("dokey"); } }

    • A.

      Ok

    • B.

      Dokey

    • C.

      Ok dokey

    • D.

      Compilatioon error

    Correct Answer
    B. Dokey
    Explanation
    The & operator has a higher precedence than the | operator so that on line 8 b1 and b2 are evaluated together as are b2 & b3. The final b1 in line 10 is what causes that if test to be true. Hence it prints "dokey".

    Rate this question:

  • 12. 

    The expression (y >= z && a == b) is evaluated by first evaluating the expression y >= z, and then evaluating a == b.   

    • A.

      True

    • B.

      False

    Correct Answer
    B. False
    Explanation
    If y >= z is false, then there is no need to evaluate the second expression.

    Rate this question:

  • 13. 

    Which statements about the output of the following program are true? public class EqualTest { public static void main(String args[]) { String s1 = “YES”; String s2 = “YES”; if ( s1 == s2 ) System.out.println(“equal”); String s3 = new String(“YES”); String s4 = new String(“YES”); if ( s3 == s4 ) System.out.println(“s3 eq s4”); } }  

    • A.

      “equal” is printed, “s3 eq s4” is printed.

    • B.

      “equal” is printed only.

    • C.

      “s3 eq s4” is printed only.

    • D.

      Nothing is printed.

    Correct Answer
    B. “equal” is printed only.
    Explanation
    The compiler creates one String object for both s1 and s2, thus “equal” appears. But using the new String operator two distinct objects are created so “s3 eq s4” does not appear.

    Rate this question:

  • 14. 

    What is the difference between >> and >>>?

    • A.

      The >> operator shifts left and the >>> operator shifts right.

    • B.

      The >> operator shifts right and the >>> operator shifts left.

    • C.

      The >> operator fills the shifted out high order bits based on the sign bit and the >>> operator fills the high order bits with zeros.

    • D.

      The >>> operator fills the shifted out high order bits based on the sign bit and the >>> operator fills the high order bits with zeros.

    Correct Answer
    C. The >> operator fills the shifted out high order bits based on the sign bit and the >>> operator fills the high order bits with zeros.
    Explanation
    The >> operator fills the shifted out high order bits based on the sign bit, meaning that if the number being shifted is positive, the high order bits will be filled with zeros, and if the number is negative, the high order bits will be filled with ones. On the other hand, the >>> operator always fills the high order bits with zeros, regardless of the sign of the number being shifted.

    Rate this question:

  • 15. 

    Null is a keyword in java

    • A.

      True

    • B.

      False

    Correct Answer
    B. False
    Explanation
    null is not a keyword but it is a reserved literal in java.

    Rate this question:

  • 16. 

    Which of the following are Java modifiers?

    • A.

      Public

    • B.

      Private

    • C.

      Transient

    • D.

      Friendly

    • E.

      Vagrant

    Correct Answer(s)
    A. Public
    B. Private
    C. Transient
    Explanation
    The keyword transient is easy to forget as is not frequently used. Although a method may be considered to be friendly like in C++ it is not a Java keyword.

    Rate this question:

  • 17. 

    Given the following variables, which of the following lines will compile without error? String s = "Hello"; long l = 99; double d = 1.11; int i = 1; int j = 0; Choose 2  

    • A.

      String result = d + i;

    • B.

      Double result = i / j;

    • C.

      Int result = l * d;

    • D.

      String result = s + l; Continue to next questionSubmit Quiz Which of the f...  Given the fo...The character pair ?...Default value of the...Extended assignment ...Which of the followi...Which of the followi...If you try to access...When you convert a d...Choose which is not ...Choose the options t...Choose the operation...Which three are vali...What is the numerica...Which one of these l...Which is a valid key...Which is a reserved ...Which three are vali...What will be the out...String is a primitiv...The operations y >...  ...The expression (y &g...Which statements abo...What is the differen...null is a keyword in...Which of the followi...What will be output ...What will be output ...

    • E.

      J=i< Continue to next questionSubmit Quiz Which of the f...  Given the fo...The character pair ?...Default value of the...Extended assignment ...Which of the followi...Which of the followi...If you try to access...When you convert a d...Choose which is not ...Choose the options t...Choose the operation...Which three are vali...What is the numerica...Which one of these l...Which is a valid key...Which is a reserved ...Which three are vali...What will be the out...String is a primitiv...The operations y >...  ...The expression (y &g...Which statements abo...What is the differen...null is a keyword in...Which of the followi...What will be output ...What will be output ...

    • F.

      J=i< Continue to next questionSubmit Quiz Which of the f...  Given the fo...The character pair ?...Default value of the...Extended assignment ...Which of the followi...Which of the followi...If you try to access...When you convert a d...Choose which is not ...Choose the options t...Choose the operation...Which three are vali...What is the numerica...Which one of these l...Which is a valid key...Which is a reserved ...Which three are vali...What will be the out...String is a primitiv...The operations y >...  ...The expression (y &g...Which statements abo...What is the differen...null is a keyword in...Which of the followi...What will be output ...What will be output ...

    Correct Answer(s)
    C. Int result = l * d;
    D. String result = s + l; Continue to next questionSubmit Quiz Which of the f...  Given the fo...The character pair ?...Default value of the...Extended assignment ...Which of the followi...Which of the followi...If you try to access...When you convert a d...Choose which is not ...Choose the options t...Choose the operation...Which three are vali...What is the numerica...Which one of these l...Which is a valid key...Which is a reserved ...Which three are vali...What will be the out...String is a primitiv...The operations y >...  ...The expression (y &g...Which statements abo...What is the differen...null is a keyword in...Which of the followi...What will be output ...What will be output ...
    F. J=i< Continue to next questionSubmit Quiz Which of the f...  Given the fo...The character pair ?...Default value of the...Extended assignment ...Which of the followi...Which of the followi...If you try to access...When you convert a d...Choose which is not ...Choose the options t...Choose the operation...Which three are vali...What is the numerica...Which one of these l...Which is a valid key...Which is a reserved ...Which three are vali...What will be the out...String is a primitiv...The operations y >...  ...The expression (y &g...Which statements abo...What is the differen...null is a keyword in...Which of the followi...What will be output ...What will be output ...
    Explanation
    The lines that will compile without error are:

    A) String result = s + l;
    D) int result = l * d;

    Explanation:

    A) In option A, we are concatenating a String `s` with a `long` value `l`. This is allowed in Java, as the `+` operator can be used for String concatenation.

    D) In option D, we are multiplying a `long` value `l` with a `double` value `d`. The result is assigned to an `int` variable. This operation is allowed in Java, and it will implicitly convert the result to an `int`.

    Option B and C would result in compilation errors for the following reasons:

    B) In option B, we are trying to assign the result of adding a `double` value `d` and an `int` value `i` to a String variable. This operation is not allowed as it attempts to mix data types in an incompatible way.

    C) In option C, we are performing an integer division with the variables `i` and `j,` where `j` is assigned a value of 0. This would result in a runtime exception (ArithmeticException) since division by zero is not allowed in Java.

    So, options A and D are the correct choices that will compile without error.

    Rate this question:

  • 18. 

    What will be output by the following line? System.out.println(Math.floor(-2.1));  

    • A.

      -2

    • B.

      2.0

    • C.

      -3

    • D.

      -3.0

    Correct Answer
    D. -3.0
    Explanation
    The Math.floor() function returns the largest integer less than or equal to a given number. In this case, -2.1 is rounded down to -3, so the output will be -3.0.

    Rate this question:

  • 19. 

    What will be output by the following line of code? System.out.println(010|4);

    • A.

      14

    • B.

      0

    • C.

      6

    • D.

      12

    Correct Answer
    D. 12
    Explanation
    As well as the binary OR objective this questions requires you to understand the octal notation which means that the leading letter zero (not the letter O)) means that the first 1 indicates the number contains one eight and nothing else. Thus this calculation in decimal mean
    8|4
    To convert this to binary means
    1000

    0100

    ----

    1100

    ----
    Which is 12 in decimal
    The | bitwise operator means that for each position where there is a 1, results in a 1 in the same position in the answer.

    Rate this question:

  • 20. 

     Which of the following has highest operator precedence  

    • A.

      ++

    • B.

      ?:

    • C.

    • D.

      &&

    • E.

      =

    Correct Answer
    A. ++
    Explanation
    The increment operator (++), which increases the value of a variable by 1, has the highest operator precedence. This means that it is evaluated first before any other operators in an expression.

    Rate this question:

  • 21. 

      Given the following variables char c = 'c'; int i = 10; double d = 10; long l = 1; String s = "Hello"; Which of the following will compile without error?

    • A.

      C=c+i;

    • B.

      S+=i;

    • C.

      I+=s;

    • D.

      C+=s;

    Correct Answer
    B. S+=i;
    Explanation
    Only a String acts as if the + operator were overloaded

    Rate this question:

  • 22. 

    The character pair ?: is called the relational operator. 

    • A.

      True

    • B.

      False

    Correct Answer
    B. False
    Explanation
    ?: is not a relational operator, it is a logical operator

    Rate this question:

  • 23. 

    Default value of the bolean is false

    • A.

      True

    • B.

      False

    Correct Answer
    A. True
    Explanation
    The default value of a boolean is false. This means that if a boolean variable is not assigned a value, it will automatically be set to false. Therefore, the statement "True" is incorrect because the default value of a boolean is not true, but false.

    Rate this question:

  • 24. 

    Extended assignment operators (for example +=) do an implicit cast. byte b = 10; b += 10; 

    • A.

      True

    • B.

      False

    Correct Answer
    A. True
    Explanation
    Extended assignment operators, such as +=, perform an implicit cast. In this case, the variable b is of type byte and the value being added is an int. Since int has a higher range than byte, an implicit cast is performed to convert the int value to byte. Therefore, the statement b += 10 is valid and will not result in a compilation error. The value of b will be 20 after the operation.

    Rate this question:

  • 25. 

    Which of the following are legal identifiers. Choose all that apply

    • A.

      2variable

    • B.

      Variable2

    • C.

      _whatavariable

    • D.

      _3_

    • E.

      $anothervar

    • F.

      #myvar

    Correct Answer(s)
    B. Variable2
    C. _whatavariable
    D. _3_
    E. $anothervar
    Explanation
    these options are valid because they are starting with alphabet,underscore(_) and dollar sign($).
    the other two options are not valid as they start with number and # symbol

    Rate this question:

  • 26. 

    Which of the following cannot be typecasted iin java

    • A.

      Byte

    • B.

      Char

    • C.

      Boolean

    • D.

      Float

    • E.

      Double

    Correct Answer(s)
    A. Byte
    B. Char
    C. Boolean
    Explanation
    In Java, primitive data types can be typecasted to each other except for boolean. Boolean is a data type that can only have two possible values: true or false. It cannot be converted or typecasted to any other data type because it is a distinct type on its own. Therefore, byte, char, and boolean cannot be typecasted in Java.

    Rate this question:

  • 27. 

    If you try to access a local variable that is not initialized then  

    • A.

      It is automatically initialized to some garbage value

    • B.

      Shows a run time error

    • C.

      Assigns a null value to the variable

    • D.

      Shows compile time error

    Correct Answer
    D. Shows compile time error
    Explanation
    If you try to access a local variable that is not initialized, it will show a compile time error. This means that the code will not be able to compile and run successfully because accessing an uninitialized variable can lead to unpredictable behavior and can cause bugs in the program. Therefore, it is considered a best practice to always initialize variables before using them to avoid such errors.

    Rate this question:

  • 28. 

    When you convert a data that has a large type to a smaller type, you must use an explicit cast. True or false  

    • A.

      True

    • B.

      False

    Correct Answer
    A. True
    Explanation
    When converting a data type with a larger size to a smaller type, it is necessary to use an explicit cast. This is because the smaller data type may not be able to hold the full range of values that the larger data type can accommodate. By using an explicit cast, you are indicating to the compiler that you are aware of the potential loss of data and are intentionally converting the value to a smaller type.

    Rate this question:

  • 29. 

    Choose which is not a characteristic of instanceof operator  

    • A.

      The instanceof operator is a binary operator that determines whether an object reference is an instance of the class, interface, or array type specified by the right operand.

    • B.

      The instanceof operator can be used with primitive types.

    • C.

      The instanceof operator returns a boolean value of true if the left operand references a non-null object of class

    • D.

      The instanceof operator returns false if none of the preceding conditions are met or if the left operand is null.

    Correct Answer
    B. The instanceof operator can be used with primitive types.
    Explanation
    The instanceof operator cannot be used for primitive datatype

    Rate this question:

  • 30. 

    Choose the options that are true about features of instance variable. Choose any 2  

    • A.

      Cannot be initialized before invoking any constructor

    • B.

      Accessible anywhere in the class

    • C.

      Static variables are initialized at class load time

    • D.

      Cannot have the same name as the class

    Correct Answer(s)
    B. Accessible anywhere in the class
    C. Static variables are initialized at class load time
    Explanation
    Instance variables are accessible anywhere in the class, meaning they can be accessed and used by any method or constructor within the class. Static variables, on the other hand, are initialized at class load time, which means they are initialized only once when the class is first loaded into memory.

    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
  • Nov 07, 2023
    Quiz Edited by
    ProProfs Editorial Team
  • Dec 05, 2011
    Quiz Created by
    311570
Back to Top Back to top
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.