Java Fundamentals Test

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 311570
3
311570
Community Contributor
Quizzes Created: 3 | Total Attempts: 5,170
| Attempts: 4,603 | Questions: 30
Please wait...
Question 1 / 30
0 %
0/100
Score 0/100
1. When you convert a data that has a large type to a smaller type, you must use an explicit cast. True or false  

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.

Submit
Please wait...
About This Quiz
Java Fundamentals Test - Quiz

This JAVA FUNDAMENTALS TEST assesses knowledge in Java programming, covering string operations, float declarations, character ranges, Java keywords, and reserved words. Ideal for learners aiming to enhance their... see moreJava programming skills. see less

2. String is a primitive datatype

Explanation

String is an object, it isn't a primitive type at all, just an array of chars.

Submit
3. Default value of the bolean is false

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.

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

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.

Submit
5. What is the difference between >> and >>>?

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.

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

Explanation

The "+" and "+ =" operators are overlaoded in Java. They join the two operands into one String object.

Submit
7. Which is a valid keyword in java?

Explanation

In Java, interface is a valid keyword used to define an interface, which is a blueprint for classes that specifies a set of methods that must be implemented by any class that implements the interface. The other options, string, Float, and unsigned, are not valid keywords in Java. string should be String, Float should be float, and unsigned is not a keyword in Java.

Submit
8. The character pair ?: is called the relational operator. 

Explanation

?: is not a relational operator, it is a logical operator

Submit
9.
  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");
    }
}

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".

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

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

Submit
11.  Which of the following has highest operator precedence  

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.

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

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.

Submit
13. Which is a reserved word in the Java programming language?

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'.

Submit
14. Null is a keyword in java

Explanation

null is not a keyword but it is a reserved literal in java.

Submit
15. What is the numerical range of a char?  

Explanation

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

Submit
16. 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");
}
}  

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.

Submit
17. Choose which is not a characteristic of instanceof operator  

Explanation

The instanceof operator cannot be used for primitive datatype

Submit
18.   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?

Explanation

Only a String acts as if the + operator were overloaded

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

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.

Submit
20. 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;

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

Submit
21. Which of the following are Java modifiers?

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.

Submit
22.
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);
    }
}

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.

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

Explanation

If y >= z is false, then there is no need to evaluate the second expression.

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

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.

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

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.

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

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

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

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.

Submit
28. Which of the following cannot be typecasted iin java

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.

Submit
29. Which three are valid declarations of a char?

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'.

Submit
30. 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  

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.

Submit
View My Results

Quiz Review Timeline (Updated): Jan 12, 2025 +

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

  • Current Version
  • Jan 12, 2025
    Quiz Edited by
    ProProfs Editorial Team
  • Dec 05, 2011
    Quiz Created by
    311570
Cancel
  • All
    All (30)
  • Unanswered
    Unanswered ()
  • Answered
    Answered ()
When you convert a data that has a large type to a smaller type, you...
String is a primitive datatype
Default value of the bolean is false
Extended assignment operators (for example +=) do...
What is the difference between >> and >>>?
Choose the operations that can be performed on String objects:...
Which is a valid keyword in java?
The character pair ?: is called the relational operator. 
 ...
The operations y >> 3 and y >>> 3 produce the same...
 Which of the following has highest operator precedence...
If you try to access a local variable that is not initialized then...
Which is a reserved word in the Java programming language?
Null is a keyword in java
What is the numerical range of a char?  
Which statements about the output of the following program are true?...
Choose which is not a characteristic of instanceof operator...
 ...
Which one of these lists contains only Java programming language...
Which three are valid declarations of a float?...
Which of the following are Java modifiers?
What will be the output of the program? ...
The expression (y >= z && a == b) is evaluated by first...
Choose the options that are true about features of instance variable....
What will be output by the following line of code?...
Which of the following are legal identifiers. Choose all that apply
What will be output by the following line?...
Which of the following cannot be typecasted iin java
Which three are valid declarations of a char?
Given the following variables, which of the following lines will...
Alert!

Advertisement