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. Prints : MyThread: start() followed by MyRunnable:run()

  2. Prints : MyThread: run() followed by MyRunnable:start()

  3. Prints : MyThread: start() followed by MyRunnable:start()

  4. Compile time error

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

myThread overrides start(), so calling myThread.start() executes its custom method, printing 'MyThread: start()'. thread is initialized with myRunnable, so calling thread.start() executes myRunnable.run(), printing 'MyRunnable: run()'.

Multiple choice technology programming languages
  1. Compile-time error

  2. prints : 3

  3. prints : 1

  4. prints : 7

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

Initially, static variable x is 0. x-- decrements it to -1. In myMethod(), y = x++ + ++x evaluates as -1 + 1 = 0, leaving x at 1. Back in main, x + y + ++x evaluates to 1 + 0 + 2 = 3.

Multiple choice technology programming languages
  1. 15 0 20

  2. 15 0 15

  3. 20 0 20

  4. 0 15 20

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

Java passes object references by value. In second(), the local parameter 'v' is reassigned to a new Value object (v = val), which doesn't affect the original reference in first(). However, before reassignment, v.i = 20 modifies the object's field through the original reference. The second() method prints '15 0' (val.i=15, local i=0). Back in first(), v.i is 20 (modified by second() before reassignment), so it prints '20'. Final output: '15 0 20'. The reassignment in second() doesn't affect first()'s reference.

Multiple choice technology programming languages
  1. 300

  2. 240

  3. 120

  4. Compilation error

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

The code compiles and runs successfully. myChi.addMe(10, 20, 30) returns 10 + 10 + 20 + 20 + 30 + 30 = 120. myChi.addMe(myChi) calls addMe(10, 20, 30) on itself, returning 120. myPar.addMe(myPar) returns 10 + 10 + 20 + 20 = 60. The sum is 120 + 120 + 60 = 300.

Multiple choice technology programming languages
  1. The code won't compile

  2. "Some things are true in this world" will be printed

  3. "Hey this won't compile" will be printed

  4. None of these

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

The nested if structure is: if(a) { if(b) { if(c) {...} else {...} } else if(a && (b=c)) {...} else {...} }. With a=true, b=false, c=true: The outer if(a==true) succeeds. Inside, if(b==true) fails (b is false), so we go to else if(a && (b=c)). Here, (b=c) assigns c to b, making b=true, so the condition (true && true) is true, printing "It's too confusing to tell what is true and what is false". This output is not listed in options A, B, or C, so D is correct.

Multiple choice technology programming languages
  1. Same

  2. Equals

  3. The code compiles, but nothing is displayed upon execution.

  4. The code fails to compile

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

The code creates two separate String objects using 'new String("Test")'. The == operator compares object references, which are different for distinct objects, so s1==s2 is false. The equals() method compares string content, and both strings contain 'Test', so s1.equals(s2) is true. The code prints only 'Equals'. This tests the fundamental distinction between reference equality (==) and content equality (equals()).

Multiple choice technology web technology
  1. Runtime Error

  2. Compilation Error

  3. Program compiles and runs properly.

  4. None of the above

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

Java allows any order for method modifiers. 'static public void' and 'public static void' are identical to the compiler. The main method requires public, static, void return type, String[] args, but modifier order is flexible.

Multiple choice technology programming languages
  1. 2

  2. 2 3

  3. Compilation fails.

  4. 1 2 3

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

The first if condition (x==4) && !b2 evaluates to false, so line 18 is skipped. Line 19 is not inside any block, so it prints 2. The second if condition performs an assignment b2 = true which evaluates to true, and since b1 is true, line 21 executes and prints 3.

Multiple choice technology programming languages
  1. Compilation fails due to an error in line 23.

  2. Compilation fails due to an error in line 29.

  3. A ClassCastException occurs in line 29.

  4. A ClassCastException occurs in line 31.

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

To solve this question, the user needs to understand the concepts of sorting arrays and the differences between the Object and Integer classes.

The code creates an array of Objects that includes Integer, String, and Boolean objects, then sorts the array using the Arrays.sort() method. Finally, the code prints out each object in the sorted array using a for loop.

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

A. Compilation fails due to an error in line 23. This option is incorrect because there are no errors in the syntax of line 23. The code creates an array of objects and initializes it with Integer, String, and Boolean objects.

B. Compilation fails due to an error in line 29. This option is incorrect because there are no errors in the syntax of line 29. The Arrays.sort() method is a valid method and can be used to sort arrays.

C. A ClassCastException occurs in line 29. This option is correct. The code attempts to sort an array of Objects, which includes Integer, String, and Boolean objects. When the sort method attempts to compare the elements in the array, a ClassCastException occurs because the elements are not all of the same type. The Integer objects are not comparable to the String and Boolean objects, resulting in an exception.

D. A ClassCastException occurs in line 31. This option is incorrect. The for loop uses the toString() method to print out each object in the array, which is a valid method for all objects. However, the exception occurs during the sort method in line 29, not during the printing of the objects.

The Answer is: C

Multiple choice technology programming languages
  1. Compilation fails.

  2. An exception is thrown at runtime

  3. doStuffx = 6 main x = 6

  4. doStuffx = 5 main x = 5

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

To solve this question, the user needs to understand the basic concept of method invocation and parameter passing.

First, let's go through the code. The Pass class has a method main which initializes an integer variable x to 5. It then creates a new instance of the Pass class and invokes the doStuff method on that instance, passing in the value of x. Finally, it prints out the value of x.

The doStuff method takes an integer parameter x, prints out the value of x incremented by 1, and then increments the parameter x by 1.

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

A. Compilation fails - This option is incorrect. There are no syntax errors in the code.

B. An exception is thrown at runtime - This option is incorrect. There are no runtime errors in the code.

C. doStuffx = 6 main x = 6 - This option is incorrect. The value of x in main is still 5 after the doStuff method is called because Java is a pass-by-value language. The parameter x in doStuff is a copy of the value of x in main, so incrementing the parameter does not affect the original value of x.

D. doStuffx = 5 main x = 5 - This option is correct. The doStuff method prints out the value of x (which is 5) incremented by 1, resulting in the output "doStuff x = 6". However, the value of x in main remains 5 because Java is a pass-by-value language. Therefore, the final output is "main x = 5".

Therefore, the answer is: D. doStuffx = 5 main x = 5

Multiple choice technology programming languages
  1. The code compiles and the output is 2

  2. If lines 16, 17 and 18 were removed, the code would compile and

  3. If lines 24, 25 and 26 were removed, the code would compile and the output would be 1

  4. D. If lines 24, 25 and 26 were removed, compilation would fail

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

The code compiles and outputs 2 because the local class A inside testFoo (lines 24-26) shadows the class A at lines 16-18. When fubar(new A()) is called, it uses the local A which returns 2. Removing lines 16-18 doesn't break compilation because the local A inside testFoo still implements Foo and satisfies the interface requirement. Removing lines 24-26 would cause fubar(new A()) to fail because A would no longer be defined in testFoo's scope - it would need to be Beta.A.

Multiple choice technology programming languages
  1. test

  2. null

  3. Compilation fails because of an error in line 1

  4. Compilation fails because of an error in line 5

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

To solve this question, the user needs to understand the concept of anonymous inner classes and the toString() method.

Explanation of the code:

  • An interface TestA is defined in line 1 with a single method toString().
  • In line 4, an anonymous inner class is created that implements the TestA interface and overrides the toString() method to return the string "test".
  • In line 6, the toString() method of the anonymous inner class is called, which returns "test".
  • In line 4, the anonymous inner class is passed as an argument to the println() method of System.out, which prints the value returned by the toString() method to the console.

Options Explanation:

  • Option A: This option is correct because the output of the program is "test", which is returned by the toString() method of the anonymous inner class.
  • Option B: This option is incorrect because the output of the program is not null.
  • Option C: This option is incorrect because there is no error in line 1. The interface TestA is defined correctly.
  • Option D: This option is incorrect because there is no error in line 5. The toString() method is overridden correctly.

The answer is: A. test