Java Mock Test 1

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: 94

SettingsSettingsSettings
Java Mock Test 1 - Quiz


Keywords,operators,primitive datatypes,variables and typecasting


Questions and Answers
  • 1. 

       Which of the following lines will compile without warning or error.

    • A.

      Float f=1.3;

    • B.

      Char c="a";

    • C.

      Byte b=257;

    • D.

      Boolean b=null;

    • E.

      int i=10;

    Correct Answer
    E. int i=10;
    Explanation
    The line "int i=10;" will compile without warning or error because it declares an integer variable "i" and assigns it the value of 10, which is a valid operation in Java. The other lines will not compile without warning or error because they have type mismatches or invalid assignments.

    Rate this question:

  • 2. 

    A byte can be of what size

    • A.

      8 bits

    • B.

      16 bits

    • C.

      32 bits

    • D.

      64 bits

    Correct Answer
    A. 8 bits
    Explanation
    A byte is a unit of digital information that consists of 8 bits. Each bit can represent a binary value of either 0 or 1. Therefore, a byte can store 8 binary digits, allowing for a total of 256 different possible values.

    Rate this question:

  • 3. 

    Which of the following are not keywords or reserved words in Java?

    • A.

      If

    • B.

      Then

    • C.

      Goto

    • D.

      While

    • E.

      Case

    Correct Answer
    B. Then
    Explanation
    The keyword "then" is not a keyword or reserved word in Java. Java does not have a "then" keyword. It is commonly used in other programming languages like Pascal and BASIC, but not in Java.

    Rate this question:

  • 4. 

    String is a primitive datatype 

    • A.

      True

    • B.

      False

    Correct Answer
    B. False
    Explanation
    The given statement is false because string is not a primitive datatype. In most programming languages, string is considered as a complex or composite datatype, as it is a sequence of characters. Primitive datatypes typically include integers, floating-point numbers, booleans, and characters, but not strings.

    Rate this question:

  • 5. 

    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 reason why "equal" is printed only is because when the strings s1 and s2 are declared and assigned the value "YES", they are both pointing to the same string object in the string pool. Therefore, when the if statement checks if s1 is equal to s2 using the == operator, it returns true and "equal" is printed. However, when s3 and s4 are declared and assigned the value "YES" using the new keyword, they are creating two separate string objects in the heap memory. Therefore, when the if statement checks if s3 is equal to s4 using the == operator, it returns false and "s3 eq s4" is not printed.

    Rate this question:

  • 6. 

    Which of the following is true about && operator?

    • A.

      The && operator is equivalent to &2.

    • B.

      The && operator always evaluates both operands.

    • C.

      The && operator is equivalent to &*2.

    • D.

      The && operator does not evaluate its second operand if the value of the first operand is false.

    Correct Answer
    D. The && operator does not evaluate its second operand if the value of the first operand is false.
    Explanation
    The correct answer is that the && operator does not evaluate its second operand if the value of the first operand is false. This means that if the first operand is false, the second operand is not checked and the overall result of the expression will be false regardless of the value of the second operand. This behavior is known as short-circuiting and can be useful in situations where evaluating the second operand may have side effects or be computationally expensive.

    Rate this question:

  • 7. 

    What value is assigned to x as the result of executing the following code? int y = 3; y++; x = y++;

    • A.

      3

    • B.

      4

    • C.

      5

    • D.

      Error

    Correct Answer
    B. 4
    Explanation
    The value assigned to x is 4 because the code first assigns the value of y (which is 3) to x. Then, the y variable is incremented by 1 using the y++ statement. So, the final value of y becomes 4, and that is the value assigned to x.

    Rate this question:

  • 8. 

    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 sign bit is 1 (indicating a negative number), the high order bits will be filled with 1s. On the other hand, the >>> operator fills the high order bits with zeros, regardless of the sign bit.

    Rate this question:

  • 9. 

    Which of the following is a  legal operation?

    • A.

      S3=s1 + s2;

    • B.

      s3=s1-s2;

    • C.

      s3=s1 & s2;

    • D.

      s3=s1 && s2

    Correct Answer
    A. S3=s1 + s2;
    Explanation
    The correct answer is s3=s1 + s2;. This is a legal operation because it is performing addition on two variables, s1 and s2, and assigning the result to s3.

    Rate this question:

  • 10. 

    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 before any constructor is invoked. Therefore, the correct options are "Accessible anywhere in the class" and "Static variables are initialized at class load time".

    Rate this question:

  • 11. 

    Which of the following will output -4.0

    • A.

      System.out.println(Math.floor(-4.7));

    • B.

      System.out.println(Math.round(-4.7));

    • C.

      System.out.println(Math.ceil(-4.7));

    • D.

      System.out.println(Math.min(-4.7));

    Correct Answer
    C. System.out.println(Math.ceil(-4.7));
    Explanation
    The correct answer is System.out.println(Math.ceil(-4.7)). This is because the Math.ceil() function returns the smallest integer greater than or equal to a given number. In this case, -4.7 is rounded up to -4.0.

    Rate this question:

  • 12. 

    .In case of Logical AND, if exp1 is false, then the operator never evaluates exp2. True or False  

    • A.

      True

    • B.

      False

    Correct Answer
    A. True
    Explanation
    In the case of Logical AND, if exp1 is false, then the operator never evaluates exp2. This means that if the first expression is false, the second expression is not checked or evaluated. Therefore, the statement is true.

    Rate this question:

  • 13. 

    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 with primitive types. It can only be used with object references.

    Rate this question:

  • 14. 

    The only data that cannot be typecasted is

    • A.

      Int

    • B.

      Float

    • C.

      Boolean

    • D.

      Double

    Correct Answer
    C. Boolean
    Explanation
    The boolean data type in programming represents true or false values. It is a primitive data type and cannot be typecasted into any other data type. Typecasting is the process of converting one data type into another. However, boolean values cannot be converted into any other data type because they only have two possible values. Therefore, the correct answer is boolean.

    Rate this question:

  • 15. 

    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 that has a large type to a smaller type, an explicit cast is required. This is because the smaller type may not be able to accommodate the full range of values that the larger type can hold. By using an explicit cast, you are indicating to the compiler that you are aware of the potential loss of data and are intentionally truncating or rounding the value to fit within the smaller type. Therefore, the correct answer is true.

    Rate this question:

  • 16. 

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

    • A.

      It is automatically initialized to some garbage value

    • B.

      It throws a compile time error

    • C.

      Shows run time error

    • D.

      Returns a null value to the variable

    Correct Answer
    B. It throws a compile time error
    Explanation
    When you try to access a local variable that is not initialized, it throws a compile-time error. This means that the compiler detects that the variable has not been assigned a value before it is being used, and it raises an error to prevent any potential issues or unexpected behavior in the program.

    Rate this question:

  • 17. 

    Byte and short, cannot be converted to char or the vice versa

    • A.

      True

    • B.

      False

    Correct Answer
    A. True
    Explanation
    In Java, byte and short are smaller data types compared to char. When converting from byte or short to char, there is a possibility of losing information because char can hold a wider range of values. Therefore, such conversions are not allowed by the Java language. Similarly, when converting from char to byte or short, there is a possibility of losing information as well. Hence, the statement that byte and short cannot be converted to char or vice versa is true.

    Rate this question:

  • 18. 

    Is null a keyword in java. 

    • A.

      True

    • B.

      False

    Correct Answer
    B. False
    Explanation
    The correct answer is False. "null" is not a keyword in Java. It is a literal that represents the absence of a value or a null reference. Keywords in Java are reserved words that have a specific meaning and cannot be used as identifiers.

    Rate this question:

  • 19. 

    Which of the following is not a variable name?

    • A.

      Name

    • B.

      Null

    • C.

      _sum

    • D.

      2sqrt

    • E.

      Count number

    Correct Answer(s)
    B. Null
    C. _sum
    D. 2sqrt
    E. Count number
    Explanation
    The variable name "Null" is not a valid variable name because it is a reserved keyword in many programming languages and cannot be used as a variable name. Similarly, the variable name "2sqrt" is not a valid variable name because it starts with a number, which is not allowed in most programming languages. The variable name "_sum" is a valid variable name because it starts with an underscore and is followed by letters or numbers. The variable name "count number" is not a valid variable name because it contains a space, which is not allowed in most programming languages.

    Rate this question:

  • 20. 

    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
    The question asks for legal identifiers, which are names used to identify variables, functions, or other entities in programming. In most programming languages, identifiers must follow certain rules. They can start with a letter or an underscore, and the rest of the characters can be letters, numbers, or underscores. They cannot start with a number or contain special characters like # or $. Therefore, the legal identifiers in this question are variable2, _whatavariable, _3_, and $anothervar.

    Rate this question:

  • 21. 

    Which one of the following is not a token? 

    • A.

      Keyword

    • B.

      Method

    • C.

      Literal

    • D.

      Identifier

    Correct Answer
    B. Method
    Explanation
    A method is not considered a token because tokens are the smallest individual units of a program, and a method is a collection of statements and code that performs a specific task. Tokens include keywords, literals, and identifiers, which are all individual components that make up a program.

    Rate this question:

  • 22. 

     Local variables are not automatically initialized. State True or False

    • A.

      True

    • B.

      False

    Correct Answer
    A. True
    Explanation
    Local variables in programming languages are not automatically initialized with default values. This means that when a local variable is declared, it does not have a predefined value and will contain whatever value is stored in that memory location at that time. Therefore, it is true that local variables are not automatically initialized.

    Rate this question:

  • 23. 

    The size of a long integer data type is: 

    • A.

      8 bits

    • B.

      16 bits

    • C.

      32 bits

    • D.

      64 bits

    Correct Answer
    D. 64 bits
    Explanation
    A long integer data type typically refers to a data type that can store larger integer values than a regular integer data type. In most programming languages, a long integer is usually 64 bits in size. This means that it can store integer values ranging from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. Therefore, the correct answer is 64 bits.

    Rate this question:

  • 24. 

    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.

      J=i

    • B.

      J=i

    • C.

      J=i

    • D.

      J=i

    Correct Answer(s)
    B. J=i
    D. J=i
    Explanation
    The given code snippet initializes variables s, l, d, i, and j with their respective values. The line "j=i" assigns the value of variable i to variable j, which is of type int. Since both variables are of the same type, the assignment will compile without error. Therefore, the lines "j=i" and "j=i" will compile without error.

    Rate this question:

  • 25. 

    Which one of the following is not true in the case of Type declaration statement? 

    • A.

      Any number of variables can be declared in a Type declaration statement.

    • B.

      Different types of variables can be declared in a single Type declaration statement

    • C.

      The list of variables in a Type declaration statement should be separated by commas.

    • D.

      The Type declaration statement can be used to declare as well as initialize a variable.

    Correct Answer
    B. Different types of variables can be declared in a single Type declaration statement
    Explanation
    In a Type declaration statement, different types of variables cannot be declared together. Each variable must have the same data type. Therefore, the statement "Different types of variables can be declared in a single Type declaration statement" is not true.

    Rate this question:

  • 26. 

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

    • A.

      True

    • B.

      False

    Correct Answer
    A. True
    Explanation
    When using extended assignment operators, such as +=, the value on the right side of the operator is implicitly cast to the type of the variable on the left side. In this case, the variable b is of type byte, and the value 10 on the right side is an int. However, since the += operator does an implicit cast, the value 10 is automatically cast to a byte before being added to b. Therefore, the statement b += 10; is valid and will not result in a compilation error. Hence, the correct answer is True.

    Rate this question:

  • 27. 

    State whether True or false. The default value for a boolean member variable is true. 

    • A.

      True

    • B.

      False

    Correct Answer
    B. False
    Explanation
    The default value for a boolean member variable is false, not true. When a boolean member variable is declared but not assigned a value, it automatically defaults to false.

    Rate this question:

  • 28. 

    The character pair ?: is called the relational operator. 

    • A.

      True

    • B.

      False

    Correct Answer
    B. False
  • 29. 

     Which of the following has highest operator precedence

    • A.

      ++

    • B.

      ?:

    • C.

    • D.

      &&

    • E.

      =

    Correct Answer
    A. ++
    Explanation
    The operator "++" has the highest precedence among the given options. This means that it is evaluated first before any other operators in an expression. The "++" operator is used to increment the value of a variable by 1.

    Rate this question:

  • 30. 

    On what situations can you use the ‘==‘ operator?

    • A.

      It can be used instead of equals()

    • B.

      To compare two primitives, use the == operator.

    • C.

      To see if two references are the same,

    • D.

      To assign a value to the variable

    Correct Answer(s)
    A. It can be used instead of equals()
    B. To compare two primitives, use the == operator.
    C. To see if two references are the same,
    Explanation
    The '==' operator can be used instead of the equals() method to compare two objects for equality. It can also be used to compare two primitive data types for equality. Additionally, the '==' operator can be used to check if two references are referring to the same object in memory. Lastly, the '==' operator can be used to assign a value to a variable.

    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
  • Dec 14, 2023
    Quiz Edited by
    ProProfs Editorial Team
  • Dec 02, 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.