C Programming Review

Question 1

  • Incorrect Definition: Solution: A situation in which something is hidden.
  • Correct Definitions:
    • Algorithm: A way to find out a solution.
    • Data: Values used to describe information; information is the mean of data.
    • Information: Knowledge about something.

Question 2

  • Correct Syntax, Incorrect Results: A C program compiling without errors or warnings means the syntax is correct.
  • Logic/Semantic Errors: The program can still produce incorrect results due to logic or semantic errors.

Question 3

  • Types of Buses in a Computer:
    • Address Bus
    • Control Bus
    • Data Bus
  • Non-Existent Bus:
    • Signal Bus

Question 4

  • Translation to Machine Language: A compiler translates a C source file into machine language.

Question 5

  • Basic Number Systems and Bases:
    • Decimal: Base-10
    • Binary: Base-2
    • Octal: Base-8
    • Hexadecimal: Base-16

Question 6

  • const Keyword with Function Parameters: Prevents the function from modifying the parameter's value.

Question 7

  • C Code Output:
#include <stdio.h>
void main() {}
int i= 10, j = 3, k = 3;
printf("%d %d ", i, j, k);
  • Correct Output: 10 3

Question 8

  • Expression !1:
    • Result: 0 (! is the logical NOT operator. !1 evaluates to false, which is represented as 0).

Question 9

  • Code Snippet:
#include <stdio.h>
#define MONTHS_IN_YEAR 12
int main() {
    const int WORKING_DAYS = 22;
    printf("%d ", MONTHS_IN_YEAR);
    printf("%d", WORKING_DAYS);
    return 0;
}
  • Output: 12 22

Question 10

  • Code Issue:
char str[1];
gets(str);
printf("%s", str);
  • Issue: No Issue (However, using gets() function is dangerous and can lead to buffer overflow. It is recommended to use fgets() instead.)

Question 11

  • strcmp() Function: Compares two strings character by character.
  • Return Value if Strings are Equal: 0

Question 12

  • C Program Output:
int a = 4, b = 2;
printf("a^b = %d", a^b);
  • Output: 6 (^ is the bitwise XOR operator. 4 XOR 2 is 6).

Question 13

  • C Program Output:
int default = 5, a = 3;
if(a > 2)
    printf("%d", default);
  • Compilation Error (default is a reserved keyword in C).

Question 14

  • Variable Declaration in C:
    • Variables must be declared before use.
    • Declaration specifies name and data type.
    • Variables are not always stored on the stack.
    • Reading values is not done with an OUT statement.
    • Scope is the range of program statements that can access the variable.

Question 15

  • Term for Type and Name of Variable/Constant: Declaration

Question 16

  • Incorrect Statement:
    • double and goto are keywords for declaring data type. (goto is a control flow statement, not a data type.)

Question 17

  • Benefit of #define with Magic Values: Improves code portability.

Question 18

  • Different Result:
    • char c2 = 066; printf("%c", c2); (Prints a different character than the others.)

Question 19

  • C Code Output:
#include <stdio.h>
void main() {
    int total=0;
    for(int s=1; s<15; s++)
        total=total+s;
    printf("%d", total);
}
  • Output: 105 (sum of numbers from 1 to 14)

Question 20

  • Incorrect Iteration Construct: (condition)? True_Value: False_Value (This is a conditional expression, not an iteration construct.)

Question 21

  • While Loop Condition Not Met: The loop will not work (if the condition is initially false).

Question 22

  • Real-World Example of switch Statement: Check the day of the week.

Question 23

  • Loop Condition Always True: Loop infinitely.

Question 24

  • Code Output:
int i = 0;
while (i < 5) {
    printf("%d ", i);
    i-- ;
}
  • Infinite Loop

Question 25

  • Technique for Solving Complex Problems: Modularity (breaking the problem into smaller programs).

Question 26

  • Program Output:
#include<stdio.h>
int main() {
    char i, j;
    for (i = 'A'; i < 'E'; i++){
        for (j = 'A'; j <= i; j++) {
        }
        printf("%c", i);
        printf("\n");
    }
    return 0;
}
  • Output:
A
BB
CCC
DDDD

Question 27

  • Function in C: is_prime()

Question 28

  • C Code Output:
#include <stdio.h>
void main() {
    int *ptr, a = 10;
    ptr = &a;
    *ptr += 1;
    printf("%d,%d\n", *ptr, a);
}
  • Output: 11,11 (The pointer ptr points to a, so changing *ptr also changes a.)

Question 29

  • Function Evaluation:
double g (double x, double y){
    return (x-2)*(x - 2) + y*y;
}
z=g(2,0);
  • Value of z: 0 (g(2, 0) = (2-2)(2-2) + 00 = 0)

Question 30

  • Incorrect Function Prototype: int printSum(int a, b); (Missing type for parameter b)

Question 31

  • Return Statement with Double Expression, Int Return Type: Conversion from double to int (truncation).

Question 32

  • islower() Function: Returns non-zero if a character is a lowercase letter.

Question 33

  • Code Output:
int a = 0;
char line1 = "Hi";
char line2] = "Hello";
a = strcmp(line1, line2);
  • Value of a: A positive value (strcmp returns a positive value if the first string is greater than the second string.)

Question 34

  • floor() Function:
    • floor(-5.9): -6
    • floor(5.9): 5

Question 35

  • sizeof(arr) / sizeof(arr[0]): Evaluates to the number of elements in arr

Question 36

  • Variable Storing Address of Another Variable: Pointer

Question 37

  • Valid Two-Dimensional Array Declaration:
int a[][3] = {{1,2,3},{4,5,6}};

Question 38

  • C Code Output:
#include <stdio.h>
void main() {
    char result = 125;
    result = result + 5;
    printf("%d", result);
}
  • Output: -126 (Character overflow. char is typically signed, with a range of -128 to 127. When 5 is added to 125 it exceeds the range of the char.)

Question 39

  • Numbered Storage Location: Address

Question 40

  • Operators Applicable to Pointers: Increments

Question 41

  • Program Output:
void func(int *p){
    int j=11;*p = j;
}
int main() {
    int i = 1;
    int *p = &i;
    func(p);
    printf("%d,%d", i, *p);
    getchar();
    return 0;
}
  • Output: 11,11 (The function func modifies the value pointed to by p, which is i.)

Question 42

  • Read Single Character from Standard Input: getchar()

Question 43

  • Character to Mark End of String: '\0' (null terminator).

Question 44

  • Concatenate Two Strings: strcat()

Question 45

  • Mode for Appending Data to a File: "a"

Question 46

  • Commands to Write Data at the End of a File:
    • FILE *f = fopen("output.txt", "a");
    • FILE *f = fopen("output.txt", "a+");

Question 47

  • Function to Read a Line From File: fgets

Question 48

  • Code Output:
char words[4][11];
strcpy(words[0], "apple");
strcat(words[0], "juice");
printf("%s\n", words[0]);
  • Output: applejuice

Question 49

  • Compilation vs. Interpretation: Compilation converts source code to machine code, while interpretation executes code line by line.

Question 50

  • rewind(fp): Brings the pointer fp back to the beginning of the file.