Questions Related to java

Multiple choice java
  1. The class is fully encapsulated

  2. The code demonstrates polymorphism

  3. The ownerName variable breaks encapsulation

  4. The cardID and limit variables break polymorphism

Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

Proper encapsulation requires all fields to be private with controlled access through methods. The ownerName field is public, breaking encapsulation. CardID and limit are correctly private. Option A is wrong (not fully encapsulated). Option B is wrong (no polymorphism shown). Option D is wrong (private fields don't break polymorphism).

Multiple choice java
  1. 0

  2. 10

  3. 12

  4. print statement never be executed

Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

To solve this question, the user needs to know about while loop and comparison operators.

In the given code snippet, the while loop will only execute if the condition inside the parentheses is true. The code initializes the variable x to 12 and checks if it is less than 10. Since 12 is greater than 10, the condition is false, and the code inside the while loop will never execute. The print statement will execute outside of the loop, which will output the value of the variable x.

Options A, B, and C can be eliminated since none of them represent the correct value.

Option D is incorrect because the print statement will execute regardless of whether the while loop is executed or not.

Therefore, the correct answer is:

The Answer is: C. 12

Multiple choice java
  1. Compilation fails

  2. Pi is approximately 3

  3. Pi is approximately 3.141593

  4. An exception is thrown at runtime

Reveal answer Fill a bubble to check yourself
D Correct answer
Explanation

The %d format specifier expects an integer, but Math.PI is a double (approximately 3.141593). This type mismatch causes an IllegalFormatConversionException at runtime. The code compiles without issues, but throws an exception when executed.

Multiple choice java
  1. The value of b is 2

  2. The value of b is 2.00

  3. The value of a is 3.1415

  4. The value of a is 3.141

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

NumberFormat.setMinimumFractionDigits(2) ensures at least 2 decimal places are displayed. Integer 2 becomes 2.00. The value 3.1415926 gets rounded to 4 decimal places (maximum setting), giving 3.1415, not 3.141 as option C suggests.

Multiple choice java
  1. a b

  2. b c

  3. a b c

  4. Compilation fails

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

Java command line arguments passed to the main method do not include the class name itself, so yahoo contains ["a", "b", "c"]. The loop starts at index 1 (x = 1), skipping yahoo[0] ("a"), thus printing yahoo[1] ("b") and yahoo[2] ("c") separated by spaces.

Multiple choice java
  1. null

  2. zero

  3. some

  4. Compilation fails

Reveal answer Fill a bubble to check yourself
D Correct answer
Explanation

To solve this question, the user needs to know that the given code is testing whether a string value is null or empty. They also need to know the syntax of if-else statements in Java.

Now, let's go through each option and explain why it is right or wrong:

A. null: This option is incorrect because the code checks if the string is null using the expression str == null, which evaluates to false since str is "null" (a non-null string literal).

B. zero: This option is incorrect because the code does not check if the string is empty using the expression str.length() == 0. Instead, it tries to use an invalid syntax by placing an else block after the if block. This leads to a compilation error.

C. some: This option is incorrect because the code won't reach this block due to the compilation error mentioned above.

D. Compilation fails: This option is correct. The code will fail to compile due to the invalid syntax in the else block. The correct syntax for an if-else statement is:

if (condition) {
    // code block for true case
} else {
    // code block for false case
}

Therefore, the correct code should be:

public static void main(String[] args) {
  String str = "null"; 
  if (str == null) {
    System.out.println("null");
  } else if (str.length() == 0) {
    System.out.println("zero");
  } else {
    System.out.println("some");
  }  
}

The Answer is: D

Multiple choice java
  1. Compilation fails

  2. An exception is thrown at runtime

  3. The variable first is set to null

  4. The variable first is set to elements[0]

Reveal answer Fill a bubble to check yourself
D Correct answer
Explanation

The array elements is initialized with three items, so its length is 3, which is greater than 0. The ternary operator evaluates the condition elements.length > 0 as true, so it assigns elements[0] to the variable first. No exceptions or compilation errors occur.

Multiple choice technology java
  1. 50

  2. ‘\u0000'

  3. cannot be determined

  4. always null until a value is

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

To answer this question, you need to understand how arrays work in Java. Specifically, the behavior of character arrays when they are initialized.

In Java, when you create an array, all elements in the array are automatically initialized to their default values. For the char data type, the default value is the null character, represented as '\u0000'.

Let's go through each option to understand why it is correct or incorrect:

Option A) 50 - This option is incorrect. When an array of char type is created in Java, its elements are not automatically initialized to their indices. Therefore, c[50] is not equal to 50.

Option B) ‘\u0000' - This option is correct. As mentioned above, when a char array is created in Java, its elements are automatically initialized to the null character, '\u0000'. Therefore, c[50] is equal to '\u0000'.

Option C) cannot be determined - This option is incorrect. In Java, the behavior of array initialization is well-defined. For a char array, all elements are automatically initialized to '\u0000'. Therefore, the value of c[50] can be determined.

Option D) always null until a value is - This option is incorrect. In the context of Java, "null" usually refers to a reference that does not point to any object. But for primitive types, such as char, the concept of "null" does not apply. The default value for a char is '\u0000', not "null".

The correct answer is B) ‘\u0000'. This option is correct because in Java, when a char array is initialized, all of its elements are automatically set to the null character, '\u0000'.