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 technology security
  1. fstack-protector

  2. fstack-protector-all

  3. fdelete-null-pointer-checks

  4. Both a and b

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

-fstack-protector-all protects all functions (most secure). -fstack-protector only protects functions with character arrays or calls to alloca(). Option C is unrelated to stack protection. Option D is wrong because A is not the best option.

Multiple choice technology security
  1. Heap Overflow

  2. Integer overflow

  3. Buffer overflow

  4. No Vulnerability

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

j and k are unsigned char (0-255). Adding two can exceed 255, wrapping around due to integer overflow. Example: j=200, k=100 gives result=44 (not 300). This is an arithmetic overflow, not a buffer overflow or heap issue.

Multiple choice technology security
  1. j=getchar();

  2. gets(ipstring);

  3. Both

  4. None

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

gets() has no buffer length check and writes past ipstring's 80-byte boundary. This is a classic buffer overflow. getchar() reads one character into j, which is safe. Option C is wrong because getchar() is not unsafe.

Multiple choice technology security
  1. This is a double free vulnerability and must be fixed

  2. The second call to free() will return an error

  3. There might be compiler warnings, but the program will run fine

  4. This is not a security issue

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

Double-free corrupts the heap allocator's internal state, potentially enabling arbitrary code execution. It is a serious security vulnerability. Option B is wrong (free() on already-freed memory crashes or corrupts, doesn't return error). Option C and D incorrectly downplay the risk.

Multiple choice technology security
  1. delete ptr2; (within if loop) AND delete ptr1;

  2. delete ptr1; AND delete ptr2; (outside if loop )

  3. delete ptr2; (within if loop)

  4. delete ptr2; (outside if loop )

Reveal answer Fill a bubble to check yourself
C Correct answer
Multiple choice technology security
  1. delete j;

  2. realloc j;

  3. free j;

  4. It need not be deleted

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

The pointer j is assigned from argv[1], which points to memory managed by the system as part of the process's argument vector. This memory should NOT be freed, deleted, or reallocated by the application - it belongs to the C runtime. Using free(), delete, or realloc on argv pointers causes undefined behavior. Option D is correct - it need not be deleted.

Multiple choice technology security
  1. delete j

  2. free j

  3. it is not supposed to be deleted

  4. delete [] j

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

The code allocates memory using 'new char[100]'. In C++, memory allocated with 'new[]' must be deallocated using 'delete[]'. However, the code contains a logic error where 'j' is reassigned to 'argv[1]', causing a memory leak and making 'delete[] j' dangerous.

Multiple choice technology security
  1. Overwriting freed memory is a security vulnerability

  2. Depends on the application and how important “somedata” is

  3. This will result in a buffer overflow since the freed memory location cannot handle 8 characters of data “somedata”

  4. strcpy() will fail as it cannot write to already freed memory, and the application will crash

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

Writing to freed memory (use-after-free) is a security vulnerability because the freed memory may have been reallocated for other purposes. An attacker could potentially manipulate the freed memory between free() and the strcpy, causing the strcpy to corrupt critical data structures, inject shellcode, or cause other security issues. Option A is correct. Option B is wrong - it IS a vulnerability regardless of data importance. Option C is wrong - 8 characters fit fine in 200 bytes. Option D is wrong - strcpy doesn't detect freed memory and will succeed, causing the vulnerability.

Multiple choice technology security
  1. Line 1

  2. Line 5

  3. Line 7

  4. Line 9

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

Line 1 sets bIsAdmin = true before authentication. If isAdminUser() throws an exception (line 5), the catch block (line 7-10) only logs the error - bIsAdmin remains true, granting unauthorized admin access. This is a logic error where default permissions are too permissive. The exception handling should set bIsAdmin to false.

Multiple choice technology security
  1. It is returning a value in finally block

  2. It is catching Exception

  3. OPTION 1 AND Option 2

  4. Nothing is wrong

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

Returning from finally blocks is dangerous because it discards any pending exceptions from try or catch blocks and creates confusing control flow. It also swallows the original return values.

Multiple choice technology programming languages
  1. for(int i=10;i>0;i-=-2);

  2. for(int i=0;i<012;i=i++);

  3. for(int i=0;i<0;i--);

  4. for(int i=0;(i++^--i)==0;i++);

  5. for(int i=010;i==10;i+=0);

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

Option B is infinite because i=i++ uses post-increment incorrectly - i is assigned its old value each time, so i stays 0 forever. Option D is infinite because the XOR (i++ ^ --i) compares a number with itself after increment/decrement cancel out, and any number XOR itself equals 0, making the condition always true. Option A terminates (i decreases from 10 to 2 in 5 steps). Option C never runs (condition false initially). Option E never runs (010 is octal 8, not 10).