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
-
int i = 0XCAFE;
-
boolean b = 0;
-
char c = A;
-
byte b = 128;
-
char c = "A";
A
Correct answer
Explanation
Option A is valid: '0XCAFE' is a hexadecimal literal (hex digits start with 0X or 0x). Option B is invalid: boolean can only be 'true' or 'false', not 0. Option C is invalid: 'A' must be quoted as a character literal. Option D is invalid: byte range is -128 to 127, 128 overflows. Option E is invalid: char can't take a String literal.
B
Correct answer
Explanation
This code is illegal because you cannot assign an int array to a long array reference. Even though long can hold int values, array types are not compatible - an int[] is not a subtype of long[]. You would need to create a new long array and copy elements individually.
-
a. The switch and case statement
-
b. ternary operator
-
c. nested if statement
-
d. The #endif statement
A
Correct answer
Explanation
The switch-case statement replaces multiple if-else statements when comparing a single expression against many constant values. This improves readability and can be more efficient. Ternary operators only replace simple if-else, and nested ifs are the opposite of simplification.
-
a. continue;
-
b. break;
-
c. return "Infant";
-
d. return 0;
C
Correct answer
Explanation
The function declares a string return type but has no return statement for ages 4 and below. Adding 'return "Infant";' as the last line ensures all execution paths return a string, satisfying the compiler's requirement that non-void functions must return a value on all paths.
-
a. true
-
b. false
-
c. sometimes
-
d. depends
B
Correct answer
Explanation
The first enum member defaults to 0, not 1, even though the underlying type is integer. This is true for C#, C++, Java, and most other languages. The default type is indeed integer, but the automatic value assignment starts at 0 unless explicitly specified.
-
a. true
-
b. false
-
c. an exception is thrown
-
d. depends
C
Correct answer
Explanation
Unboxing extracts a value type from an object reference. If that reference is null, there's nothing to extract, causing a runtime exception (NullReferenceException in C#). You cannot unbox null because unboxing requires an actual boxed value to retrieve.
-
a. Yes
-
b. No
-
c. Under certain conditions
-
d. May be
B
Correct answer
Explanation
Only ONE catch block executes for a given exception. Exception handlers are evaluated in order, and the first matching catch block handles the exception. Once executed, control jumps to the finally block (if any) and then continues after the try-catch structure. No other catch blocks run for that same exception.
-
a. readonly value can be changed later
-
b. const items are dealt at compile time however readonly is dealt at runtime
-
c. const value can be changed later
-
d. const items are dealt at runtime however readonly is dealt at compile time
B
Correct answer
Explanation
const is a compile-time constant - the value is embedded at compile time and cannot change. readonly is a runtime constant that can be assigned once (at declaration or in a constructor) and then becomes immutable. This is why const can only be used for value types and string literals, while readonly can reference any type.
-
memory
-
register
-
variable
-
assembler
D
Correct answer
Explanation
Operands are the data values that instructions operate on. Memory locations, registers, and variables can all serve as operands as they hold or reference data values. An assembler is a software tool that converts assembly language to machine code - it is not an operand but rather a program that processes instructions containing operands.
B
Correct answer
Explanation
Java's reserved words are: if, else, while, for, case, goto (reserved but unused), switch, etc. 'then' is NOT a Java keyword - it's used in some other languages but not in Java's syntax. This makes 'then' the correct answer.
-
Address of the array
-
Values of the first elements of the array
-
Address of the first element of the array
-
Number of element of the array
C
Correct answer
Explanation
When an array is passed to a function, it decays to a pointer to its first element. This is why array parameters are often declared as pointers, and why sizeof() returns the pointer size not the array size inside functions. Option C correctly states this: arrays are passed as the address of their first element.
-
float ptr;
-
float *ptr;
-
*float ptr;
-
None of the above
B
Correct answer
Explanation
In C, a pointer to float is declared using an asterisk (*) before the variable name: float *ptr;. Option A (float ptr;) declares a regular float variable, not a pointer. Option C (*float ptr;) has incorrect syntax - the asterisk must come after the type, not before it.
-
exception
-
harness
-
assertion
-
expression
C
Correct answer
Explanation
An assertion is a statement that a programmer believes should always be true at a specific point in code execution. Assertions are used as debugging and testing aids to check assumptions during development, and typically trigger an error or exception if the condition evaluates to false.
B
Correct answer
Explanation
Java does not use 'then' as a keyword. Words like 'if', 'while', 'case' are keywords. 'goto' is a reserved word (not used but reserved for future potential use).
-
At least one variable must be added to a Global Variable group
-
The predefined value of a Global Variable cannot be overridden from the command line
-
The predefined value of a Global Variable for individual services can be changed at deployment time
-
When using a Global Variable in the fields of a resource, enter the name surrounded by $
A,C
Correct answer
Explanation
A Global Variable group must contain at least one variable to be valid. Global Variable values can be overridden at deployment time for individual services. Global Variables can be overridden from the command line using the -var option, and they are referenced in resources using the syntax %%globalVariableName%%, not with $.