Computer Knowledge

Programming Output Evaluation

1,721 Questions

Programming output evaluation tests the ability to trace code execution in languages like C, Java, and SAS. It focuses on arrays, loops, pointers, and data type conversions. These technical questions are standard in computer knowledge sections for IT officer and bank exams.

Java string bufferC language pointersLoop execution outputsData type conversionsMacro variable evaluation

Programming Output Evaluation Questions

Multiple choice technology programming languages
  1. 0

  2. 256

  3. 100

  4. None of the above

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

The hexadecimal value 0x80 equals 128 in decimal. When you left-shift this by 1 position (i<<1), you multiply by 2, resulting in 256. The printf uses %d which prints the integer value, and unsigned char is promoted to int in the expression, so 256 fits within the output.

Multiple choice technology programming languages
  1. 0.0

  2. Compilation fails

  3. A ParseException is thrown by the parse method at runtime

  4. A NumberFormatException is thrown by the parse method at runtime

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

Variable f is declared inside the try block, so it's not in scope in the catch or finally blocks. Both f= 0; and System.out.println(f); cause compilation errors because f is not declared in those scopes.

Multiple choice technology programming languages
  1. 5

  2. 10

  3. 12

  4. 17

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

The parameter x in the method shadows the instance variable. Inside method(), x refers to the parameter (value 5), not the instance field (value 12). So x+=x means 5+=5, which equals 10.

Multiple choice technology programming languages
  1. Line 57 will print the value 2

  2. Line 57 will print the value 3

  3. Compilation will fail because of an error in line 55

  4. Compilation will fail because of an error in line 56

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

Array references in Java are reference types. When y[] = x executes, both variables point to the same array. Since arrays are 0-indexed, y[2] accesses the third element with value 3.

Multiple choice technology programming languages
  1. for( Color c : Color.values())

  2. for( Color c = RED; c <= BLUE; c++)

  3. for( Color c; c.hasNext() ; c.next())

  4. for( Color c = Color[0]; c <= Color[2]; c++)

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

To solve this question, the user needs to know how to iterate through an enum in Java.

Option A is correct because it uses the values() method provided by the Color enum to iterate through all the possible values, and prints each one to the console.

Option B is incorrect because it treats the Color enum as if it were an integer, which it is not. It also uses an incorrect syntax for the for loop header.

Option C is incorrect because there is no hasNext() method for enums in Java. This is a method provided by the Iterator interface, which enums do not implement.

Option D is incorrect because there is no Color[0] syntax in Java. Additionally, the enum values are not guaranteed to be stored in an array in the order they are defined.

Therefore, the correct answer is:

The Answer is: A. for( Color c : Color.values())

Multiple choice technology programming languages
  1. Mr. John Doe

  2. An exception is thrown at runtime

  3. Compilation fails because of an error in line 12

  4. Compilation fails because of an error in line 15

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

The enum constant MR has title="Mr.". When format("Doe", "John") is called, it concatenates title + " " + first + " " + last, producing "Mr. John Doe". Note that last name is passed as the first parameter and first name as the second.

Multiple choice technology programming languages
  1. 1 2 3

  2. Compilation fails because of an error in line 12.

  3. Compilation fails because of an error in line 13

  4. Compilation fails because of an error in line 14

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

To solve this question, the user needs to know about arrays and casting in Java. The code initializes an Object obj to an array of integers with values 1, 2, and 3. It then casts the object to an array of integers and assigns it to the someArray variable. Finally, it uses a for-each loop to print out each element of the someArray array.

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

A. 1 2 3: This option is correct. The code initializes an array of integers with values 1, 2, and 3, casts it to an array of integers, and then prints out each element of the array using a for-each loop. This results in the output "1 2 3".

B. Compilation fails because of an error in line 12: This option is incorrect. Line 12 initializes an object to an array of integers, which is allowed in Java. There is no compilation error in this line.

C. Compilation fails because of an error in line 13: This option is incorrect. Line 13 casts the obj object to an array of integers, which is also allowed in Java. There is no compilation error in this line.

D. Compilation fails because of an error in line 14: This option is incorrect. Line 14 uses a for-each loop to print out each element of the someArray array, which is also allowed in Java. There is no compilation error in this line.

Therefore, the answer is:

The Answer is: A. 1 2 3

Multiple choice technology programming languages
  1. Compilation of class A fails

  2. Line 28 prints the value 3 to System.out

  3. Line 28 prints the value 1 to System.out.

  4. A runtime error occurs when line 25 executes

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

Static methods cannot directly access instance variables or instance methods because they belong to the class, not to a specific object instance. The method getInstanceCount() is static but tries to access the instance variable counter, causing a compilation failure.

Multiple choice technology programming languages
  1. Line 26 prints “a” to System.out

  2. Line 26 prints ‘b” to System.out

  3. An exception is thrown at line 26 at runtime.

  4. Compilation of class A will fail due to an error in line 6

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

To solve this question, the user should understand the concept of method overloading and how Java resolves the method call based on the number and type of arguments passed.

In the given code, class A has two methods named doit. One method takes two integer arguments, and the other takes an arbitrary number of integer arguments.

When we call a.doit(4,5) on line 26, Java examines both methods in class A that are named doit and checks to see which one matches the argument types. Since there is a doit method with two integer parameters, Java selects that method, and the String "a" is returned by that method.

Therefore, the output of the given code is:

The Answer is: A. Line 26 prints “a” to System.out.

Multiple choice technology programming languages
  1. An exception is thrown at runtime

  2. Compilation fails because of an error in line 7

  3. Compilation fails because of an error in line 4.

  4. Compilation succeeds and no runtime errors with class A occur

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

Java methods are distinguished by their signature (name and parameter types), not by return type. Lines 3 and 4 declare two doit() methods with identical signatures but different return types (void vs String), which causes compilation failure. Line 7 is valid overloading because the parameter differs.

Multiple choice technology web technology
  1. Compile error

  2. printf("What this will print")

  3. What this will print

  4. 20

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

The inner printf prints the string and returns the number of characters printed (20). The outer printf then prints this return value as an integer. So the full output is 'What this will print20' on the same line, and the outer printf displays 20.