Computer Knowledge

Programming Output Evaluation

1,953 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. B,C

  2. A,I,F

  3. B,C,F

  4. None of the above

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

The nested loops iterate with i from 0 to 1 and j from 2 down to 0. When i equals j, the inner loop breaks. This produces outputs: when i=0, j prints 2 then 1 (breaks at 0); when i=1, j prints 2 then breaks at 1. The outputs are (i=0,j=2), (i=0,j=1), and (i=1,j=2).

Multiple choice technology programming languages
  1. D

  2. E

  3. A

  4. Both D and E

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

To solve this question, the user must understand the concepts of try-catch blocks and the order of exception handling.

The code first initializes two integer variables, i and j, both with the value 1. It then enters a try block where it increments i by 1 and decrements j by 1. If i and j are equal, it increments i by 1 again. However, this condition is not met, so the code skips over the try block and proceeds to the finally block, where it prints out 3. It then prints out 4.

The correct answer is D) Both D and E, because the code will output 3 and 4. The try block does not throw any exceptions, so none of the catch blocks are executed. The finally block is always executed, regardless of whether an exception is thrown or not. Therefore, the output will be 3 (from the finally block) and 4 (from the last print statement).

Multiple choice technology programming languages
  1. if((i == 0) || (j/i == 1))

  2. if((i == 0) | (j/i == 1))

  3. if((i != 0) && (j/i == 1))

  4. if((i != 0) & (j/i == 1))

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

The single bitwise operators | and & do not perform short-circuit evaluation. Therefore, even if the first operand determines the outcome, the second operand (j/i == 1) is evaluated, causing an ArithmeticException (division by zero) because i is 0.

Multiple choice technology programming languages
  1. 6 5

  2. 6 1

  3. 12 2

  4. 5 4

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

The code contains syntax errors such as a mismatched quote in the string array ('Croak!") and a typo in sizeoof. Assuming these are typos, sizeof(str) returns the total size of the pointer array (12 bytes on 16-bit systems or 24/48 on modern systems), and sizeof(str[0]) is the size of a single pointer (2 bytes on 16-bit systems).

Multiple choice technology programming languages
  1. Garbage values

  2. 2 3 3

  3. 3 2 2

  4. 0 0 0

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

In C, when you partially initialize an array, all remaining elements are automatically set to 0. The array a[5] = {2,3} means a[0]=2, a[1]=3, and a[2], a[3], a[4] are all 0. The extra semicolon is a harmless empty statement.

Multiple choice technology programming languages
  1. 1 0 1

  2. 0 0 1

  3. 1 2 3

  4. Compile error

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

The statement a[i]=i++ is undefined behavior in C because it modifies i and also uses its value in the same expression without a sequence point. However, the expected output is 0 0 1 because a[0] gets the original value of i (which is 0) before the increment, and a[1] remains uninitialized (0 for static arrays). The i variable becomes 1.

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.