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
  1. catch(X x) can catch subclasses of X where X is a subclass of Exception.

  2. The Error class is a RuntimeException.

  3. Any statement that can throw an Error must be enclosed in a try block.

  4. Any statement that can throw an Exception must be enclosed in a try block.

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

Option A is correct. If the class specified in the catch clause does have subclasses, any exception object that subclasses the specified class will be caught as well. Option B is wrong. The error class is a subclass of Throwable and not RuntimeException. Option C is wrong. You do not catch this class of error. Option D is wrong. An exception can be thrown to the next method higher up the call stack.

Multiple choice
  1. It is used to start a session.

  2. It is used to destroy the session.

  3. It is used to store a session variable.

  4. Both (1) and (3)

  5. None of the above

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

Yes, $_SESSION variable is used to store or retrieve the session variable.

Multiple choice
  1. A try statement must have at least one corresponding catch block.

  2. Multiple catch statements can catch the same class of exception more than once.

  3. An Error that might be thrown in a method must be declared as thrown by that method, or be handled within that method.

  4. Except in case of VM shutdown, if a try block starts to execute, a corresponding finally block will always start to execute.

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

A is wrong. A try statement can exist without catch, but it must have a finallystatement. B is wrong. A try statement executes a block. If a value is thrown and the try statement has one or more catch clauses that can catch it, then control will be transferred to the first such catch clause. If that catch block completes normally, then the try statement completes normally. C is wrong. Exceptions of type Error and RuntimeException do not have to be caught, only checked exceptions (java.lang.Exception) have to be caught. However, speaking of Exceptions, Exceptions do not have to be handled in the same method as the throw statement. They can be passed to another method. If you put a finally block after a try and its associated catch blocks, then once execution enters the try block, the code in that finally block will definitely be executed except in the following circumstances: An exception arising in the finally block itself. The death of the thread. The use of System.exit() Turning off the power to the CPU. I suppose the last three could be classified as VM shutdown.

Multiple choice
  1. 2, 4

  2. 3, 5

  3. 4, 5

  4. 1, 2

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

(1) causes two compiler errors ( '[' expected and illegal start of expression) because the wrong type of bracket is used, ( ) instead of [ ]. The following is the correct syntax:float[ ] f = new float[3]; (2) causes a compiler error ( '{' expected ) because the array constructor does not specify the number of elements in the array. The following is the correct syntax: float f2[ ] = new float[3]; (3), (4), and (5) compile without error.

Multiple choice
  1. In an assert statement, the expression after the colon ( : ) can be any Java expression.

  2. If a switch block has no default, adding an assert default is considered appropriate.

  3. In an assert statement, if the expression after the colon ( : ) does not have a value, the assert's error message will be empty.

  4. It is appropriate to handle assertion failures using a catch clause.

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

Adding an assertion statement to a switch statement that previously had no default case is considered an excellent use of the assert mechanism. Option A is incorrect because only Java expressions that return a value can be used. For instance, a method that returns void is illegal. Option C is incorrect because the expression after the colon must have a value. Option D is incorrect because assertions throw errors and not exceptions, and assertion errors do cause program termination and should not be handled.

Multiple choice
  1. boolean

  2. void

  3. public

  4. Button

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

In Java, 'public' is an access modifier (controls visibility), not a return type. Return types specify what data type a method returns: void (returns nothing), boolean (returns true/false), or any class type like Button. Access modifiers like public, private, protected are separate concepts that specify who can access the method, not what it returns.

Multiple choice
  1. The default char data type is a space( ' ' ) character.

  2. The default integer data type is 'int' and real data type is 'float'

  3. The default integer data type is 'long' and real data type is 'float'

  4. The default integer data type is 'int' and real data type is 'double'

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

Java has default types for numeric literals. Integer literals (like 42) are int by default. Floating-point literals (like 3.14) are double by default. Option D correctly states both defaults.

Multiple choice
  1. It debuggs a program easily.

  2. It consumes less disk space.

  3. Recursive calls to function is possible.

  4. Testing a program becomes easier.

  5. Complexity of a program is reduced.

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

This is the correct option as usage of function in the program consumes more disk space than without using a function.

Multiple choice
  1. Calling a function from another function

  2. Calling a same function with different form

  3. Calling a function from itself

  4. Calling a same function in same form every time

  5. There is no such term in C++

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

This is the correct option as calling the same function in different form allows a user to perform multiple operation with the same function and hence called as function overloading.