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

  2. False

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

In C++, you cannot declare an array of references. 'int& x[50]' attempts to create an array of 50 references to int, which is syntactically illegal. References must be initialized upon declaration and cannot be reseated, making arrays of references impossible. This code will not compile, so the statement is False.

Multiple choice technology programming languages
  1. 0

  2. 5

  3. undefined

  4. 2

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

The global const int x = 5 is used to size the array inside main (int x[x] creates array of 5 ints). sizeof(x) on the array returns 5 * sizeof(int) = 20 (assuming 4-byte int). Dividing by sizeof(int)=4 gives y = 5. The local array x shadows the global x for the rest of the block.

Multiple choice technology programming languages
  1. 20

  2. 10

  3. 100

  4. -80

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

The macro XXX expands to 'ABC - XYZ', which becomes '20 - 10'. The expression 'a = XXX * 10' becomes 'a = 20 - 10 * 10'. Due to operator precedence, multiplication happens before subtraction: 10 * 10 = 100, then 20 - 100 = -80.

Multiple choice technology programming languages
  1. 5

  2. 7

  3. 8

  4. garbage value

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

To solve this question, we need to understand the order of operations and logical operators.

The given program uses the if statement to check some conditions. If the conditions are met, it increases the value of k. Finally, it prints the value of k using printf.

The given expression can be broken down into three parts:

  1. ++k < 5
  2. k++/5
  3. ++k <= 8

Let's evaluate each part:

  1. ++k < 5: The value of k is first incremented to 6. Then, this expression checks if 6 is less than 5. This is false, so the expression evaluates to false.

  2. k++/5: The value of k is still 6. This expression divides 6 by 5 (integer division), which evaluates to 1. However, this expression is not used in the if statement because of short-circuiting.

  3. ++k <= 8: The value of k is now 7 (due to the previous increment). This expression checks if 7 is less than or equal to 8. This is true, so the expression evaluates to true.

Since the expression in the if statement evaluates to true, the code inside the if statement is executed. This code does nothing, so k remains 7. Finally, the value of k is printed using printf.

Therefore, the output of this program is:

7

The answer is: B. 7

Multiple choice technology programming languages
  1. true

  2. false

  3. compilation error

  4. exception thrown at runtime

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

When using 'new String()', a new String object is created on the heap each time, even if the character sequence is the same. s1 and s2 are two distinct objects with the same value 'abc'. The '==' operator compares object references, not content, so s1 == s2 returns false.

Multiple choice technology programming languages
  1. Compilation and output of "Not equal! 10".

  2. Compilation and output of "Not equal! 20".

  3. Compilation and output of "We are equal 10".

  4. Compilation error, attempting to perform binary comparison on logical data type.

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

The condition uses the short-circuit logical AND operator &amp;&amp;. Since b1 == true evaluates to false, the second operand ((Output += 10) == 20) is never executed. Consequently, Output remains 10, and the program enters the else block, printing "Not equal! 10".

Multiple choice technology programming languages
  1. 0 0

  2. Compilation error, class A has no start method

  3. 0 1

  4. Compilation succeed but runtime exception

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

To understand the code, the user needs to know the basics of Java threads and anonymous inner classes.

The code defines a class A that extends Thread and overrides its run() method to print the values of i from 0 to 1. It also defines a class Test with a main() method that creates an instance of Test and calls its check() method with an anonymous inner class that extends A. The check() method calls the start() method on the A instance, which starts a new thread that executes the run() method.

Now let's go through each option and explain why it is true or false:

A. 0 0: This option is false. The code creates a new thread that executes the run() method of the A instance, which prints the values of i from 0 to 1. Since the start() method is called only once, the output will be either "0 1" or "1 0", depending on which thread runs first.

B. Compilation error, class A has no start method: This option is false. The A class extends Thread, which provides the start() method. Thus, there is no compilation error.

C. 0 1: This option is true. The code creates a new thread that executes the run() method of the A instance, which prints the values of i from 0 to 1. Since the start() method is called only once, the output will be either "0 1" or "1 0", depending on which thread runs first.

D. Compilation succeed but runtime exception: This option is false. There is no runtime exception in the code.

Therefore, the correct answer is:

The Answer is: C. 0 1

Multiple choice technology programming languages
  1. run-a

  2. run-a run-a

  3. Compilation fails with an error at line 6

  4. Compilation succeed but Runtime Exception

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

Calling start() on a Thread that has already been started is illegal and causes it to throw an IllegalThreadStateException at runtime. The code itself is syntactically valid and compiles without error.

Multiple choice technology programming languages
  1. Value is 67 18 String 13

  2. Value is 13 18 String 13

  3. Value is 13 18 String

  4. Compilation fails

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

First, string concatenation evaluates "Value is " + b + c as "Value is 67". Next, numeric addition of a + b + c yields 18. Finally, the expression "String " + (b+c) evaluates the parenthesis first to get 13, printing String 13.

Multiple choice technology programming languages
  1. Compile fail due to error on line no 2

  2. Compile fail due to error on line no 9

  3. Compile fail due to error on line no 8

  4. 15

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

The code fails to compile because line 9 attempts to call the instance method add() from a static context (main method). Non-static methods must be called on an object instance. You would need to create an instance of class B (or A) with 'new B().add(s, 6)' or declare add() as static to call it directly.

Multiple choice technology programming languages
  1. 8 7

  2. 10 7

  3. Compilation fails with an error at line 3

  4. Compilation fails with an error at line 5

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

In Java, integer literals starting with 0 are interpreted as octal (base-8) numbers. Octal 010 equals decimal 8 (1×8 + 0), and octal 07 equals decimal 7. The code compiles and prints '8' then '7' - no error, just a different number system than expected.