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. the number of arguments

  2. the number of arguments plus one

  3. the number of arguments minus one

  4. The total size of the argument array

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

The argument count (argc) includes the program name as argv[0], so it's always one more than the number of command-line arguments passed. If you run ./prog arg1 arg2, argc is 3 (program name + 2 arguments). This is why argc equals number of arguments plus one, not just the argument count.

Multiple choice technology programming languages
  1. stdin.h

  2. ctype.h

  3. math.h

  4. None

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

Typecasting is a fundamental language feature in C++, not a library function. No header file is needed to use C-style casts (int)x or C++ casts static_cast(x). Options A, B, and C are all real headers but for different purposes (stdio, character types, math functions). Typecasting syntax is built into the compiler.

Multiple choice technology programming languages
  1. int to float

  2. float to int

  3. char to float

  4. All are possible

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

All listed conversions are possible in C++ between primitive types. int to float is widening (always safe). float to int truncates the fractional part. char to float converts ASCII numeric value to floating point. These are all standard type conversions available through implicit or explicit casting.

Multiple choice technology programming languages
  1. +public

  2. #protected

  3. !package

  4. -private

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

In UML class diagrams, visibility symbols are: + (public), - (private), # (protected), and ~ (package/default). Option B claims # represents protected, but # actually IS the correct UML symbol for protected. The question asks for the WRONG pair, and + for public, ! for package, and - for private are all correct. Only # for protected is correctly represented, making B the wrong statement if it claimed otherwise.

Multiple choice technology operating systems
  1. checks prime number; returns one when not prime and zero when prime

  2. checks prime number; returns none when not prime and zero when prime

  3. checks prime number; returns zero when not prime and one when prime

  4. checks prime number; returns zero when not prime and none when prime

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

This script checks if $l is prime by testing divisibility from 2 upward. If $l % i == 0 (divisible), it prints and breaks with exit status 0 (success). If no divisor found, loop completes naturally, exit status is 1 (failure) from the last comparison. The 'print' statement only executes when not prime (divisor found), so output appears when not prime; no output when prime.

Multiple choice technology operating systems
  1. checks prime number; returns one when not prime and zero when prime

  2. checks prime number; returns none when not prime and zero when prime

  3. checks prime number; returns zero when not prime and one when prime

  4. checks prime number; returns zero when not prime and none when prime

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

This is a duplicate of question 136523. The script tests if $l is prime by checking divisibility. If divisible (not prime), it prints and exits with status 0. If no divisor found (prime), loop ends with exit status 1 from the last comparison. Output appears when not prime, none when prime.

Multiple choice technology operating systems
  1. #On the beach at night, stands a child with her father

  2. #On #the #at #a #with #her

  3. #beac #nigh #stan #chil #fath

  4. #beach #night, #stands #child #father

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

The script loops through each word in the input string, checking if its length exceeds 4 characters using ${#i}. Words longer than 4 characters are printed with a '#' prefix. From the input, the qualifying words are: beach (5), night, (6 including comma), stands (6), child (5), and father (6). Option D correctly shows these words prefixed with '#'.

Multiple choice technology operating systems
  1. typset x=32+8

  2. set x=32+8

  3. let x=32+8

  4. x=32+8

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

In shell scripting, 'let' is used for arithmetic operations. The command 'let x=32+8' evaluates the arithmetic expression 32+8 and assigns the result 40 to variable x. Option A uses 'typset' which is a typo for 'typeset' (used for variable declaration), Option B 'set' is for shell options/parameters, and Option D is just a string assignment in most shells.

Multiple choice technology operating systems
  1. checks prime number; returns one when not prime and zero when prime

  2. checks prime number; returns none when not prime and zero when prime

  3. checks prime number; returns zero when not prime and one when prime

  4. checks prime number; returns zero when not prime and none when prime

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

The script checks if a number $l is prime by testing divisibility from 2 to l-1. The 'break' statement exits when a divisor is found. The key is understanding that 'print $?' displays the exit status of the last command - 'break' exits with status 0 (success), so when a divisor is found (not prime), it prints 0. When no divisor is found (prime), the loop completes normally and nothing is printed (none/empty).

Multiple choice technology programming languages
  1. True

  2. False

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

Static variables belong to the class itself rather than to any instance. They are not serialized because serialization is meant to save the state of individual objects, and static variables represent shared class-level state. Serializing them would duplicate shared state across deserialized instances, breaking the design.

Multiple choice technology programming languages
  1. Compile-Time Error

  2. a=5 b=10

  3. a=10 b=5

  4. None of the above

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

The macro definition has malformed backslashes - they should only appear at line endings for continuation, not before each statement. Also, the macro call 'Swap;' lacks proper syntax. The preprocessor will fail to parse this correctly, resulting in a compilation error.

Multiple choice technology programming languages
  1. Compile-Time Error

  2. 50 50

  3. 50 0

  4. None of the above

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

A void pointer can hold the address of any data type. When we assign &val to ptr, then cast ptr back to (int*) before dereferencing, we correctly retrieve the original value 50. The program prints the value twice - once directly from val, once through the pointer.

Multiple choice technology programming languages
  1. Compile-Time Error

  2. 5 6 30

  3. 4 7 28

  4. None of the above

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

The expression --a*++b has both operators executing before multiplication: --a decrements a from 5 to 4, ++b increments b from 6 to 7, then 4*7 = 28. printf evaluates arguments left-to-right, so a and b are printed after the increments: 4 and 7.