Computer Knowledge

Programming Fundamentals

2,611 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. as and when required

  2. at the end of the compilation process

  3. at the beginning of the compilation process

  4. while running the program

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

The C preprocessor processes directives (like #include, #define) at the beginning of compilation, before the actual compilation phase. This is why preprocessor directives can't use C syntax - they're processed by a separate program that prepares the source code for the compiler.

Multiple choice
  1. True

  2. False

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

In C++, functions can return references or pointers to variables, allowing for modifications to the original data through the return value. This is useful for operator overloading and chaining operations. However, care must be taken not to return references to local variables that go out of scope.

Multiple choice
  1. bad()

  2. good()

  3. eof()

  4. fail()

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

The fail() function returns true when a stream operation fails, including when an input/output operation on a file fails. This sets the failbit in the stream state. The bad() function is for serious errors like stream corruption, good() returns true if no errors occurred, and eof() checks for end-of-file condition specifically.

Multiple choice
  1. address of operator

  2. reference operator

  3. value operator

  4. indirection operator

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

The * operator in C++ is called the indirection operator or dereference operator because it accesses the value at the address stored in a pointer. It is the opposite of the address-of operator (&). Address and reference operators are different concepts.

Multiple choice
  1. 0

  2. 1

  3. -1

  4. None of the above

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

External variables (declared with extern keyword) are automatically zero-initialized in C/C++. This ensures global and external variables start with a predictable value when no explicit initialization is provided.

Multiple choice
  1. 0

  2. 1

  3. -1

  4. none of the above

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

In C, external (global) variables are automatically initialized to 0 if not explicitly initialized. This applies to both external variables and static variables. Local variables, however, are not automatically initialized and contain garbage values.

Multiple choice
  1. translation time

  2. compilation time

  3. run time

  4. none of the above

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

Exceptions occur during program execution (runtime) when something unexpected happens - like division by zero, null pointer access, or file not found. Compile-time errors are syntax errors caught by the compiler, while runtime exceptions happen while the program is running.

Multiple choice
  1. Part 1

  2. Part 2

  3. Part 3

  4. No Error

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

Part 2 (class=" no error") contains a syntax error - there should be no leading space before the class name. In HTML/Java applet tags, attribute values should not have leading/trailing spaces unless intended. The correct syntax would be class="noerror" or class="MyApplet.class".

Multiple choice
  1. Use sub string

  2. Use one argument

  3. Use sub str

  4. Use two arguments

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

The error occurs because Java is case-sensitive and String methods have specific names. The correct method name is 'substring()' not 'subStr()'. The space in 'sub Str' also causes compilation error. Java String objects use camelCase naming convention for methods like substring(), indexOf(), charAt().

Multiple choice
  1. constants

  2. function

  3. type definitions

  4. parables

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

In programming, a function is a self-contained module that performs a specific task or computation. Constants are fixed values, type definitions define data structures, and 'parables' is not a programming term. Functions are fundamental building blocks that promote code reusability and organization.

Multiple choice
  1. compilation

  2. debugging

  3. loading

  4. execution

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

Runtime errors occur only during program execution when the computer attempts an invalid operation (like division by zero or accessing invalid memory). Compile-time errors are caught during compilation, and debugging/loading are processes, not error-detection phases.

Multiple choice
  1. Repetition

  2. Selection

  3. Sequence

  4. Switching

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

Sequence is fundamental - every program executes statements sequentially (one after another) by default. Even the simplest program must execute statements in order. Selection (if/else) and repetition (loops) are optional control structures that programs may or may not use. "Switching" is not a standard control structure category.

Multiple choice
  1. reads a line of string from cin into mystring

  2. reads a line of string from mystring into cin

  3. cin cannot be used in this way

  4. Function declaration is wrong

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

The getline(cin, mystring) function reads a complete line of text from the input stream (cin) and stores it in the string variable mystring. Unlike cin >> mystring which stops at whitespace, getline reads until a newline character. Option B is reversed (cin is the source, not destination). Option C is incorrect because getline is a standard valid function.

Multiple choice
  1. Start ( )

  2. Begin( )

  3. Main ( )

  4. Output( )

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

main() is the designated entry point where C++ program execution begins. When you run a C++ program, the operating system calls the main() function first. Options like Start(), Begin(), or Output() are not standard entry points in C++. The exact signature is typically int main() for standards-compliant code.

Multiple choice
  1. Any modifications to the variables x & y from inside the function will not have any effect outside the function.

  2. The variables x and y will be updated when any modification is done in the function.

  3. The variables x and y will be passed back to the function addition.

  4. The result of a function cannot be assigned to a variable Z.

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

When arguments are passed by value, the function receives copies of the original values. Any modifications made to x and y inside the function affect only the local copies - the original variables outside remain unchanged. This is why pass-by-reference (using &) is needed when you want modifications to affect the originals. Option C describes pass-by-reference behavior, and option D is incorrect because function results can be assigned to variables.