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 java
  1. int[ ] scores = {3, 5, 7};

  2. int [ ][ ] scores = {2,7,6}, {9,3,45};

  3. String cats[ ] = {"Fluffy", "Spot", "Zeus"};

  4. boolean results[ ] = new boolean [] {true, false, true};

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

To determine which option will cause a compiler error, we need to understand the syntax and rules of arrays in programming languages.

An array is a data structure that allows you to store multiple values of the same type under a single variable name. In most programming languages, arrays have a fixed size and can only store elements of the same type.

Now, let's analyze each option to identify if it will cause a compiler error:

A. int[ ] scores = {3, 5, 7}; This option is correct. It declares an integer array named "scores" and initializes it with three values: 3, 5, and 7. This code will not cause a compiler error.

B. int [ ][ ] scores = {2,7,6}, {9,3,45}; This option is incorrect. It declares a two-dimensional integer array named "scores" and initializes it with two rows and three columns. However, the array initializer is incorrect. Each row of a two-dimensional array should be enclosed in curly braces. The correct syntax should be: int [ ][ ] scores = {{2,7,6}, {9,3,45}}; The given code will cause a compiler error.

C. String cats[ ] = {"Fluffy", "Spot", "Zeus"}; This option is correct. It declares a String array named "cats" and initializes it with three strings: "Fluffy", "Spot", and "Zeus". This code will not cause a compiler error.

D. boolean results[ ] = new boolean [] {true, false, true}; This option is correct. It declares a boolean array named "results" and initializes it with three boolean values: true, false, and true. This code will not cause a compiler error.

Therefore, the option that will cause a compiler error is:

The Answer is: B

Multiple choice java
  1. int[ ] ia = new int[15];

  2. float fa = new float[20];

  3. char[ ] ca = "Some String";

  4. int ia[ ] [ ] = { 4, 5, 6 }, { 1,2,3 };

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

Option A uses the correct syntax to declare and instantiate an array of integers with a size of 15. Option B attempts to assign an array to a single float variable. Option C attempts to assign a String literal to a char array (type mismatch). Option D has syntax errors in the array initialization expression.

Multiple choice java
  1. int Long

  2. Short Long

  3. Compilation fails.

  4. An exception is thrown at runtime.

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

To solve this question, the user needs to know about method overloading, widening, and boxing/unboxing conversions in Java.

Each option can be evaluated as follows:

Option A: int Long

  • The first call to go() passes in a short argument. Since there is no exact match for a short parameter, the short value is widened to an int. Thus, the first call to go() invokes the go(int n) method and the output is "int ".
  • The second call to go() passes in a long argument, which matches the go(Long n) method. Thus, the second call to go() invokes the go(Long n) method and the output is "Long ".
  • Therefore, the output of the program is "int Long".

Option B: Short Long

  • The first call to go() passes in a short argument, which matches the go(Short n) method. Thus, the first call to go() invokes the go(Short n) method and the output is "Short ".
  • The second call to go() passes in a long argument, which matches the go(Long n) method. Thus, the second call to go() invokes the go(Long n) method and the output is "Long ".
  • Therefore, the output of the program is "Short Long".

Option C: Compilation fails.

  • There are no compilation errors in the code. The program will compile and run without errors.
  • Therefore, option C is incorrect.

Option D: An exception is thrown at runtime.

  • There are no exceptions thrown in the code. The program will execute without throwing any exceptions.
  • Therefore, option D is incorrect.

Therefore, the correct answer is:

The Answer is: A. int Long

Multiple choice java
  1. foofoofoofoofoo

  2. foobarfoobarbar

  3. foobarfoofoofoo

  4. foobarfoobarfoo

  5. barbarbarbarbar

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

Static fields are shadowed, not overridden, and are resolved based on the reference type. Base.FOO and b.FOO use the Base class/reference (foo). Sub.FOO and s.FOO use the Sub class/reference (bar). The cast ((Base)s).FOO forces the use of the Base reference, resulting in 'foo'.

Multiple choice java
  1. short LONG

  2. SHORT LONG

  3. Compilation fails.

  4. An exception is thrown at runtime.

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

The method call go(z) fails because int cannot be autoboxed to Short, widened to Long, and there is no go(int) method. While go(y) works (autoboxing to Short), the presence of the invalid call go(z) causes a compilation error overall.

Multiple choice java
  1. test

  2. null

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

  4. An exception is thrown at runtime.

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

To solve this question, the user needs to understand Java syntax, specifically the concepts of interfaces, anonymous inner classes, and the toString() method.

In the given code, an anonymous inner class is created that implements the TestA interface. This inner class overrides the toString() method from the Object class and returns the string "test".

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

A. test: This option is correct. When the toString() method is called on the anonymous inner class object, it returns the string "test". This is the output that will be printed to the console.

B. null: This option is incorrect. The toString() method is overridden in the anonymous inner class to return the string "test", not null. Therefore, the output will not be null.

C. Compilation fails because of an error in line 1: This option is incorrect. There is no error in line 1. The interface TestA is defined correctly.

D. An exception is thrown at runtime: This option is incorrect. There is no exception thrown in the given code. The code will compile and run without any exceptions.

The Answer is: A. test

Multiple choice c
  1. to write output to the screen

  2. the program entry point

  3. Both correct

  4. Both wrong

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

In C, main() is the designated entry point where program execution begins. While it can perform output (A), its primary definition is the entry point (B). Option C is false because A describes a function, not the definition of main.

Multiple choice c
  1. gets( s[100] );

  2. gets( s[] );

  3. gets( s );

  4. none of the above

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

The function gets() expects a pointer to a character array (char *). Since the array name s decays to a pointer to its first element, gets(s) is the correct syntax. Option A passes an int, and B uses invalid empty bracket syntax.

Multiple choice c
  1. no output

  2. 7

  3. 3

  4. 5

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

To understand the output of this code, the user needs to know basic programming concepts, including variable assignment and the use of the if statement. The user must evaluate the code to determine the value of x and whether the if statement will execute its block of code.

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

A. no output: This option is incorrect. The code contains a printf statement, which will output the value of x if the if statement condition is true.

B. 7: This option is incorrect. The value of x is initially assigned the value 7, but this value is overwritten in the if statement condition.

C. 3: This option is correct. The if statement condition x = 3 is an assignment operation. It assigns the value 3 to x and also evaluates to 3. Since the value of x is now 3, the if statement condition is true, and the block of code will execute. The printf statement will output the value of x, which is 3.

D. 5: This option is incorrect. There is no mention of the value 5 in the code. The value assigned to x is 3, not 5.

Therefore, the correct answer is:

The Answer is: C. 3