Computer Knowledge

Programming Fundamentals

2,381 Questions

Programming fundamentals form the core logic of software development and computer science. This includes variable declarations, pointer assignments, loop iterations, and exception handling. These technical topics are regularly tested in computer knowledge and IT officer competitive examinations.

Variables and arraysPointer assignmentsLoop iterationsException handling blocksCompile time errorsFunction references

Programming Fundamentals Questions

Multiple choice technology programming languages
  1. Code compiles

  2. If line 1 is removed the code compiles

  3. If line 3 is removed the code compiles

  4. If line 5 is removed the code compiles

  5. If line 1,3 are removed the code compiles

  6. If line 1,3,5 are removed the code compiles

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

In Java, enums can only be declared at the top level or within a class, but NOT within a method. Line 5's enum D is declared inside method C(), which is illegal and causes compilation failure. Removing line 5 (option D) fixes this. Enum A at line 1 (top-level) and enum B at line 3 (inside class E2 but outside method) are both legal. Option F works because removing all enums leaves valid code.

Multiple choice technology programming languages
  1. True

  2. False

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

The code compiles fine. The ArithmeticException is a checked exception that can be thrown in this context. The main method can throw any exception since it's the entry point. The code would compile but would terminate at runtime with the exception, never reaching the println statement. Option A (True) would be the correct answer if the question asked about compilation.

Multiple choice technology programming languages
  1. once

  2. twice

  3. the program will go on into infinite loop

  4. None of the above

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

The Perl code reads inpput.txt line-by-line using a while loop. The entire content of the file is printed line-by-line, meaning the file's contents are printed exactly once. Other options like printing twice or entering an infinite loop are incorrect because the loop terminates once the end of the file is reached.

Multiple choice technology programming languages
  1. A File Handle

  2. A List

  3. A Code variable

  4. A Tyleglob

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

INPUT in Perl is conventionally used as a file handle name (all uppercase identifiers typically indicate file handles). File handles are used for input/output operations with files, pipes, or sockets. This follows Perl's naming convention where uppercase names are reserved for file handles like STDIN, STDOUT, etc.

Multiple choice technology testing
  1. the use of a variable before it has been defined

  2. unreachable (“dead”) code

  3. memory leaks

  4. array bound violations

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

Static analysis examines source code without executing it, finding issues like unused variables, dead code, and potential array bounds violations. Memory leaks are dynamic runtime anomalies caused by improper allocation/deallocation during execution, which are detected via dynamic testing rather than static analysis.

Multiple choice technology programming languages
  1. No,pointers can not be used here

  2. by using unsafe block

  3. by compiling using /unsafe option in command line only

  4. Both B and C

  5. None

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

Pointers in C# can only be used within unsafe blocks, and the code must be compiled with the /unsafe option either in the IDE or command line. Both conditions are required, making 'Both B and C' correct.

Multiple choice technology mainframe
  1. True

  2. False

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

The FOR loop is specifically designed for situations where the number of iterations IS fixed or known in advance. For dynamic condition-based termination where the count isn't predetermined, you would use REPEAT...UNTIL or WHILE...ENDWHILE loops instead. The statement reverses the actual use case.

Multiple choice technology mainframe
  1. True

  2. False

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

PERFORM is a versatile Natural statement that can execute both external subprograms (separately compiled objects) and inline subroutines (defined within the same program using the SUBROUTINE statement). This dual capability provides flexibility in code organization and reuse.

Multiple choice technology programming languages
  1. call by reference

  2. call by value

  3. call by address

  4. All the above

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

Using pointers to pass arguments to a function simulates passing by reference because it allows the function to modify the actual arguments in the caller's scope. Call by value copies the actual data, whereas call by address is a synonym but call by reference is the standard curriculum terminology.

Multiple choice technology programming languages
  1. core dump

  2. nothing

  3. undefined behavior

  4. all the above

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

In C, calling free(NULL) is explicitly defined to do nothing. The C standard (C99 7.20.3.2) states that if ptr is a null pointer, no action occurs. It does NOT cause a core dump or undefined behavior. This is different from freeing an already-freed pointer or freeing unallocated memory, which are undefined. Option B is correct - 'nothing' happens.

Multiple choice technology programming languages
  1. The instance gets garbage collected.

  2. The code on line 33 throws an exception.

  3. . The code on line 35 throws an exception

  4. The code on line 31 throws an exception

  5. The code on line 33 executes successfully

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

The finally block executes in three scenarios: B) when an exception is thrown in the try block, C) when an exception is thrown in the catch block, and E) when the try block completes successfully. Option A is false - garbage collection doesn't trigger finally. Option D is false - code before the try block (line 31) doesn't affect whether this try-catch-finally executes its finally. The finally block ALWAYS executes unless the JVM exits or there's a catastrophic failure.

Multiple choice technology programming languages
  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

In Java, compound assignment operators such as += automatically cast the result of the operation back to the type of the left-hand variable. Therefore, x += 1 is evaluated as x = (byte)(x + 1). This compiles successfully without needing an explicit cast, resulting in x having the value 1.

Multiple choice technology programming languages
  1. The output is 0

  2. Will give compile error

  3. Will give runtime error

  4. The output is 1

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

The final instance variable j is not initialized at declaration, in an instance initializer block, or within any constructor. In Java, all uninitialized final instance variables cause a compile-time error. This prevents the code from compiling, making options claiming successful execution or runtime errors incorrect.