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

  2. [,]

  3. ,,*

  4. None of the Above

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

The recursive method exampleprint(1) follows this flow: print '[' then call exampleprint(0) which prints '', then print ',', then call exampleprint(0) again which prints '', then print ']' and newline. Result: [,]

Multiple choice technology programming languages
  1. Arrays in Java are essentially objects

  2. It is not possible to assign one array to another. Individual elements of array can however be assigned.

  3. Array elements are indexed from 1 to size of array

  4. If a method tries to access an array element beyond its range, a compile warning is generated.

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

Arrays in Java are objects - they're dynamically allocated, have a length field, and inherit from Object class. Options B, C, and D are false: arrays of the same type can be assigned (reference copy), indexing starts at 0 not 1, and out-of-range access throws ArrayIndexOutOfBoundsException at runtime, not a compile warning.

Multiple choice technology programming languages
  1. Float.POSITIVE_INFINITY

  2. Double.POSITIVE_INFINITY

  3. runtime Exception

  4. None of the Above

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

In Java, Math.max(double, double) is invoked due to overload resolution converting the float argument to a double. Double.POSITIVE_INFINITY and Float.POSITIVE_INFINITY are mathematically equal, but the method returns a double type representation of positive infinity.

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

  5. Compilation fails because of an error on line 28

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

Class A fails compilation because the static method getInstanceCount() attempts to access the non-static instance variable counter. Static methods cannot access instance variables directly because they run in a static context without a reference to any specific object instance.

Multiple choice technology programming languages
  1. Compilation fails.

  2. An exception is thrown at runtime.

  3. The code executes normally and prints "bar".

  4. The code executes normally, but nothing prints.

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

This code creates an anonymous inner class implementing Runnable, then starts a Thread with it. The code compiles successfully because: 1) The anonymous inner class syntax is correct: new Runnable() { public void run() {...} } 2) The Runnable interface is properly implemented with the run() method 3) The Thread constructor accepts a Runnable 4) The start() method is called correctly on the Thread. When the thread runs, it executes the run() method, which prints 'bar' to the console.

Multiple choice technology programming languages
  1. Compilation fails.

  2. aAaA aAa AAaa AaA

  3. AAaa AaA aAa aAaA

  4. AaA AAaa aAaA aAa

  5. aAa AaA aAaA AAaa

  6. An exception is thrown at runtime

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

To solve this question, the user needs to know the basics of Java programming language and the concept of ArrayList and Collections.sort() method.

The program initializes an ArrayList of strings and adds four different string elements to it. Then, it uses the Collections.sort() method to sort the elements of the ArrayList in ascending order. Finally, it prints the sorted list of strings.

When comparing strings, the sort() method uses lexicographic ordering, meaning that it compares the strings character by character based on their Unicode values.

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

A. Compilation fails. This option is incorrect. The program is syntactically correct and will compile without errors.

B. aAaA aAa AAaa AaA. This option is incorrect. The sort() method sorts the strings in ascending order, so the correct output should start with AAaa, followed by AaA, then aAa, and finally aAaA.

C. AAaa AaA aAa aAaA. This option is correct. The sort() method sorts the strings in ascending order based on their Unicode values. Therefore, the correct output is AAaa AaA aAa aAaA.

D. AaA AAaa aAaA aAa. This option is incorrect. The correct output should start with AAaa, followed by AaA, then aAa, and finally aAaA.

E. aAa AaA aAaA AAaa. This option is incorrect. The correct output should start with AAaa, followed by AaA, then aAa, and finally aAaA.

F. An exception is thrown at runtime. This option is incorrect. There are no statements in the program that could throw any exceptions.

Therefore, the correct answer is:

The Answer is: C. AAaa AaA aAa aAaA

Multiple choice technology programming languages
  1. 234

  2. 334

  3. 2334

  4. 0123456

  5. 01234456

  6. 12334567

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

The regex \d* matches zero or more digits. On string 'ab34ef', it matches: position 0 (empty), position 1 (empty), position 2 (digits '34'), position 4 (empty), position 5 (empty). The code prints m.start() + m.group() for each match: '0' + '' + '1' + '' + '2' + '34' + '4' + '' + '5' + '' = '01234456'. The pattern matches twice at position 4 because after consuming '34', zero-width matches occur at positions 4 and 5.

Multiple choice technology programming languages
  1. List<List<Integer>> table = new List<List<Integer>>();

  2. List<List<Integer>> table = new ArrayList<List<Integer>>();

  3. List<List<Integer>> table = new ArrayList<ArrayList<Integer>>();

  4. List<List, Integer> table = new List<List, Integer>();

  5. List<List, Integer> table = new ArrayList<List, Integer>();

  6. List<List, Integer> table = new ArrayList<ArrayList, Integer>();

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

The code needs a mutable List of Lists. Option B is correct because ArrayList> implements List> due to generic type compatibility. Option A fails because List is an interface and cannot be instantiated. Options C, E, F have incorrect generic syntax. Option D is invalid syntax. The diamond operator works because the inner type parameter is inferred.

Multiple choice technology programming languages
  1. The before() method will print 1 2

  2. The before() method will print 1 2 3

  3. The before() method will print three numbers, but the order cannot be determined

  4. The before() method will not compile

  5. The before() method will throw an exception at runtime

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

TreeSet requires its elements to be mutually Comparable. The code adds String "2", Integer 3, and String "1" to a TreeSet without a Comparator. When TreeSet tries to sort these mixed types, it attempts to compare String with Integer using natural ordering, which throws ClassCastException at runtime because String and Integer are not mutually comparable.

Multiple choice technology databases
  1. Arrays in Java are essentially objects

  2. It is not possible to assign one array to another. Individual elements of array can however be assigned.

  3. Array elements are indexed from 1 to size of array

  4. If a method tries to access an array element beyond its range, a compile warning is generated.

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

Arrays in Java are objects that inherit from Object class and have a length field. Option B is false because you can assign array references (array1 = array2) - just the reference is copied, not elements. Option C is false because Java arrays are 0-indexed. Option D is false because out-of-range access throws ArrayIndexOutOfBoundsException at runtime, not a compile warning.

Multiple choice technology databases
  1. Results in x having the value 1.

  2. Causes a compiler error.

  3. Will require a cast (byte) before 1.

  4. Will give syntax error.

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

The compound assignment operator x += 1 is valid for byte type. The result of x += 1 is implicitly cast back to byte, so x will have the value 1. This is a special rule for compound assignment operators - they perform an implicit narrowing primitive conversion. No explicit cast is required, unlike simple assignment which would need (byte)1.

Multiple choice technology databases
  1. float

  2. int

  3. byte

  4. double

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

When mixing numeric types in arithmetic, Java promotes to the widest type involved: float (32-bit) + int (32-bit) + byte (8-bit) results in float. Promotion follows: byte/short/char → int → long → float → double. Since float is present, all operands are promoted to float. The result is NOT double (higher precision) or int (narrower).

Multiple choice technology programming languages
  1. 5 6 7

  2. 5 followed by an exception

  3. Compilation fails with an error on line 7

  4. Compilation fails with an error on line 8

  5. Compilation fails with an error on line 9

  6. Compilation fails with an error on line 10

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

Classes Foo and Baz are in different packages (pkgA and pkgB). Variable a has default (package-private) access, and b is protected. Since Baz is not in pkgA and does not inherit from Foo, it cannot access f.a (line 8) or f.b (line 9), resulting in compilation failures on those lines.

Multiple choice technology programming languages
  1. Compilation succeeds

  2. Compilation fails with an error on line 6

  3. Compilation fails with an error on line 7

  4. Compilation fails with an error on line 8

  5. Compilation fails with an error on line 9

  6. Compilation fails with an error on line 10

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

Line 7 is invalid because Java identifiers cannot start with '#'. Line 8 fails compilation because array dimension limits cannot be specified in the type declaration part (e.g., long [5] x or long [] x [5]). Line 10 fails compilation because local enums are not permitted inside methods in standard Java versions where this question originates.