Correct Answer: getchar()
Explanation: The `getchar()` function is used to read a single character from the standard input (keyboard).
Correct Answer: putchar()
Explanation: The `putchar()` function is used to print a single character to the standard output (screen).
Correct Answer: scanf()
Explanation: The `scanf()` function is used to read formatted input from the standard input, allowing the programmer to specify the type of input expected.
Correct Answer: printf()
Explanation: The `printf()` function is used to print formatted output to the standard output, providing control over the format of the output.
Correct Answer: %d
Explanation: The format specifier `%d` is used in `printf()` to print an integer value.
Correct Answer: fgets()
Explanation: The `fgets()` function reads a string of characters from the standard input, which is a safer alternative to `gets()` as it allows specifying the maximum number of characters to read.
Correct Answer: String
Explanation: The format specifier `%s` is used in `printf()` to print a string.
Correct Answer: gets()
Explanation: The `gets()` function is deprecated because it does not perform bounds checking, which can lead to buffer overflows.
Correct Answer: scanf(“%d”, &value);
Explanation: To read an integer value, the `%d` format specifier is used with the `scanf()` function, and the address of the variable is passed to store the input.
Correct Answer: int
Explanation: The `getchar()` function returns an `int` because it needs to be able to return any character and the special value `EOF` to indicate the end of input.
Correct Answer: Floating-point number
Explanation: The format specifier `%f` is used in `printf()` to print a floating-point number.
Correct Answer: \n
Explanation: The escape sequence `\n` is used in `printf()` and `puts()` to print a new line.
Correct Answer: fscanf()
Explanation: The `fscanf()` function is used to read formatted input from a file, similar to how `scanf()` reads from standard input.
Correct Answer: Character
Explanation: The format specifier `%c` is used in `scanf()` to read a single character from the input.
Correct Answer: sprintf()
Explanation: The `sprintf()` function is used to write formatted output to a string, similar to how `printf()` writes to standard output.
Correct Answer: Sets a minimum field width for the output
Explanation: The width specifier in `printf()`, such as `%10d`, sets a minimum field width for the output, ensuring that the printed value occupies at least 10 characters.
Correct Answer: –
Explanation: The `-` flag in `printf()` is used to left-align the output within the specified field width.
Correct Answer: %.2f
Explanation: The format specifier `%.2f` in `printf()` is used to print a floating-point number with 2 decimal places.
Correct Answer: scanf()
Explanation: The `scanf()` function is used to read a floating-point number from the standard input using the `%f` format specifier.
Correct Answer: The number of input items successfully matched and assigned
Explanation: The return value of `scanf()` represents the number of input items that were successfully matched and assigned to the provided variables.
Correct Answer: %u
Explanation: The format specifier `%u` is used in `printf()` to print an unsigned integer.
Correct Answer: %x
Explanation: The format specifier `%x` is used in `printf()` to print an integer in hexadecimal format.
Correct Answer: %o
Explanation: The format specifier `%o` is used in `printf()` to print an integer in octal format.
Correct Answer: %p
Explanation: The format specifier `%p` is used in `printf()` to print a pointer address.
Correct Answer: %ld
Explanation: The format specifier `%ld` is used in `printf()` to print a long integer.
Correct Answer: %lf
Explanation: The format specifier `%lf` is used in `scanf()` to read a double precision floating-point number.
Correct Answer: %s
Explanation: The format specifier `%s` is used in `printf()` to print a string.
Correct Answer: %5.2f
Explanation: The format specifier `%5.2f` in `printf()` sets the width to 5 and the precision to 2 for a floating-point number.
Correct Answer: %+d
Explanation: The format specifier `%+d` in `printf()` is used to print a signed integer with a leading plus or minus sign.
Correct Answer: Floating-point number in scientific notation
Explanation: The format specifier `%e` is used in `printf()` to print a floating-point number in scientific notation.
Correct Answer: fopen()
Explanation: The `fopen()` function is used to open a file in C for reading, writing, or appending.
Correct Answer: fopen(“data.txt”, “r”);
Explanation: To open a file in read mode, the correct syntax is `fopen(“data.txt”, “r”);`.
Correct Answer: “w”
Explanation: The mode `”w”` is used with `fopen()` to create a new file for writing. If the file already exists, it will be truncated.
Correct Answer: NULL
Explanation: If `fopen()` fails to open a file, it returns `NULL`.
Correct Answer: fclose()
Explanation: The `fclose()` function is used to close an opened file in C.
Correct Answer: fclose(fp);
Explanation: The correct way to close a file pointer named `fp` is `fclose(fp);`.
Correct Answer: fread()
Explanation: The `fread()` function is used to read data from a file into a buffer.
Correct Answer: fread(array, sizeof(int), 10, fp);
Explanation: The correct syntax is `fread(array, sizeof(int), 10, fp);`, where `fp` is the file pointer.
Correct Answer: fwrite()
Explanation: The `fwrite()` function is used to write data from a buffer to a file.
Correct Answer: fwrite(array, sizeof(int), 10, fp);
Explanation: The correct syntax is `fwrite(array, sizeof(int), 10, fp);`, where `fp` is the file pointer.
Correct Answer: fprintf()
Explanation: The `fprintf()` function is used to print formatted output to a file.
Correct Answer: fprintf(fp, “Hello, World!”);
Explanation: To write the string “Hello, World!” to a file, the correct syntax is `fprintf(fp, “Hello, World!”);`.
Correct Answer: fscanf()
Explanation: The `fscanf()` function is used to read formatted input from a file.
Correct Answer: fscanf(fp, “%d”, &num);
Explanation: The correct syntax to read an integer from a file using `fscanf()` is `fscanf(fp, “%d”, &num);`.
Correct Answer: All of the above
Explanation: Modes `”r+”`, `”w+”`, and `”a+”` can be used with `fopen()` to open a file for both reading and writing.
Correct Answer: fopen() returns NULL
Explanation: If you try to open a file that does not exist in `”r”` mode, `fopen()` returns `NULL`.
Correct Answer: rewind(fp)
Explanation: The `rewind(fp)` function moves the file pointer to the beginning of a file.
Correct Answer: fseek()
Explanation: The `fseek()` function is used to move the file pointer to a specific location in a file.
Correct Answer: The current position of the file pointer
Explanation: The `ftell()` function returns the current position of the file pointer.
Correct Answer: Use fseek() and ftell()
Explanation: To determine the size of a file, you can use `fseek()` to move to the end of the file and `ftell()` to get the position of the file pointer, which indicates the file size.
Correct Answer: fputc()
Explanation: The `fputc()` function is used to write a single character to a file.
Correct Answer: fgetc(fp);
Explanation: The `fgetc()` function is used to read a single character from a file.
Correct Answer: size_t
Explanation: The return type of the `fread()` and `fwrite()` functions is `size_t`, which indicates the number of items successfully read or written.
Correct Answer: Open a file for appending
Explanation: The mode `”a”` in `fopen()` is used to open a file for appending. If the file does not exist, it is created.
Correct Answer: ftell()
Explanation: The `ftell()` function is used to get the current position of the file pointer in a file.\
Correct Answer: r
Explanation: The `”r”` mode opens an existing file for reading only. If the file does not exist, the `fopen()` function fails.
Correct Answer: The file is truncated to zero length
Explanation: When a file is opened in `”w”` mode, it is truncated to zero length. If the file does not exist, it is created.
Correct Answer: a
Explanation: The `”a”` mode opens a file for appending data at the end. If the file does not exist, it is created.
Correct Answer: Opens a file for reading and writing, starting at the beginning
Explanation: The `”r+”` mode opens a file for both reading and writing, starting at the beginning of the file. If the file does not exist, the `fopen()` function fails.
Correct Answer: The file is truncated to zero length
Explanation: Opening a file in `”w+”` mode truncates the file to zero length. If the file does not exist, it is created.
Correct Answer: Opens a file for reading and writing, appending data at the end
Explanation: The `”a+”` mode opens a file for both reading and writing. It appends data at the end of the file. If the file does not exist, it is created.
Correct Answer: r+
Explanation: The `”r+”` mode allows a file to be opened for both reading and writing without truncating its content.
Correct Answer: w+
Explanation: The `”w+”` mode creates a new file for reading and writing. If the file already exists, it is truncated to zero length.
Correct Answer: a
Explanation: The `”a”` mode will create a new file if it does not exist and append data to it if it does exist.
Correct Answer: An error occurs and fopen() returns NULL
Explanation: When opening a file in `”r”` mode, if the file does not exist, an error occurs and `fopen()` returns `NULL`.
Correct Answer: a+
Explanation: The `”a+”` mode ensures that a new file is created if it does not exist and existing content is preserved while allowing for both reading and writing.
Correct Answer: a
Explanation: In `”a”` mode, the file pointer moves to the end of the file before writing starts, allowing data to be appended.
Correct Answer: a+
Explanation: The `”a+”` mode opens an existing file for both reading and writing and starts at the end of the file, allowing for appending.
Correct Answer: NULL
Explanation: If you try to open a file in `”w”` mode but the file system is read-only, `fopen()` will return `NULL`.
Correct Answer: r+
Explanation: The `”r+”` mode should be used to modify the contents of an existing file without truncating it, allowing for both reading and writing.
Correct Answer: The file is truncated to zero length
Explanation: Opening a file in `”w+”` mode truncates the file to zero length, effectively erasing any existing content.
Correct Answer: End of the file
Explanation: In `”a+”` mode, the file pointer moves to the end of the file after each write operation, ensuring that data is always appended.
Correct Answer: w
Explanation: The `”w”` mode opens a file for writing and will overwrite any existing content if the file already exists.
Correct Answer: At the end of the file
Explanation: When a file is opened in `”a”` mode, any new data written to it will be placed at the end of the file.
Correct Answer: a+
Explanation: The `”a+”` mode allows you to read from and write to a file, with all write operations appending data at the end of the file.
Correct Answer: Creates a new file for writing, but fails if the file already exists
Explanation: The `”x”` mode creates a new file for writing, but if the file already exists, the `fopen()` function fails.
Correct Answer: x
Explanation: The `”x”` mode will cause `fopen()` to fail if the file already exists, ensuring that a new file is created only if it does not already exist.
Correct Answer: a+
Explanation: The `”a+”` mode opens a file for both reading and writing, ensuring that the file is created if it does not exist, and all content is appended at the end.
Correct Answer: x
Explanation: The `”x”` mode safely creates a new file for writing and will fail if the file already exists, ensuring that no existing content is truncated.
Correct Answer: “r+” opens an existing file for reading and writing without truncating, while “w+” creates a new file or truncates an existing file for reading and writing.
Explanation: The `”r+”` mode opens an existing file for reading and writing without truncating its content, whereas `”w+”` mode creates a new file or truncates an existing file for reading and writing.
#include <stdio.h>
int main() {
int x = 7;
printf("%d\n", x);
return 0;
}
Correct Answer: 7
Explanation: The `printf` function prints the value of the variable `x`, which is 7.
#include <stdio.h>
int main() {
char ch = 'A';
printf("%c\n", ch);
return 0;
}
Correct Answer: A
Explanation: The `printf` function with the `%c` format specifier prints the character value stored in `ch`, which is ‘A’.
#include <stdio.h>
int main() {
int a = 10, b = 20;
printf("%d %d\n", a, b);
return 0;
}
Correct Answer: 10 20
Explanation: The `printf` function prints the values of `a` and `b`, which are 10 and 20 respectively.
#include <stdio.h>
int main() {
float f = 3.14;
printf("%.2f\n", f);
return 0;
}
Correct Answer: 3.14
Explanation: The `printf` function with the `%.2f` format specifier prints the float value `f` with two decimal places, which is 3.14.
#include <stdio.h>
int main() {
printf("%d\n", printf("Hello"));
return 0;
}
Correct Answer: Hello5
Explanation: The inner `printf` prints “Hello” and returns the number of characters printed (5). The outer `printf` prints this return value (5) followed by a newline.
#include <stdio.h>
int main() {
int a = 5;
scanf("%d", &a);
printf("%d\n", a);
return 0;
}
Assume the input provided by the user is 10.
Correct Answer: 10
Explanation: The `scanf` function reads an integer from the user and stores it in `a`. The `printf` function then prints the value of `a`, which is 10.
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("You entered: %d\n", num);
return 0;
}
Assume the input provided by the user is 42.
Correct Answer: Enter a number: 42 You entered: 42
Explanation: The first `printf` prompts the user to enter a number. The `scanf` reads the input, and the second `printf` prints the entered number.
#include <stdio.h>
int main() {
int x;
x = printf("Hello");
printf("%d\n", x);
return 0;
}
Correct Answer: Hello5
Explanation: The first `printf` prints “Hello” and returns the number of characters printed (5). The second `printf` prints this return value (5) followed by a newline.
#include <stdio.h>
int main() {
char str[10];
printf("Enter a string: ");
scanf("%s", str);
printf("You entered: %s\n", str);
return 0;
}
Assume the input provided by the user is “world”.
Correct Answer: Enter a string: world You entered: world
Explanation: The first `printf` prompts the user to enter a string. The `scanf` reads the input, and the second `printf` prints the entered string.
#include <stdio.h>
int main() {
char ch;
printf("Enter a character: ");
scanf("%c", &ch);
printf("You entered: %c\n", ch);
return 0;
}
Assume the input provided by the user is ‘A’.
Correct Answer: Enter a character: A You entered: A
Explanation: The first `printf` prompts the user to enter a character. The `scanf` reads the input, and the second `printf` prints the entered character.
#include <stdio.h>
int main() {
int num1, num2;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
printf("You entered: %d and %d\n", num1, num2);
return 0;
}
Assume the input provided by the user is “15 30”.
Correct Answer: Enter two numbers: 15 30 You entered: 15 and 30
Explanation: The first `printf` prompts the user to enter two numbers. The `scanf` reads the input, and the second `printf` prints the entered numbers.
#include <stdio.h>
int main() {
float f;
printf("Enter a float: ");
scanf("%f", &f);
printf("You entered: %.2f\n", f);
return 0;
}
Assume the input provided by the user is “3.1415”.
Correct Answer: Enter a float: 3.1415 You entered: 3.14
Explanation: The first `printf` prompts the user to enter a float. The `scanf` reads the input, and the second `printf` prints the entered float value rounded to two decimal places.
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
int result = scanf("%d", &num);
printf("scanf returned: %d\n", result);
return 0;
}
Assume the input provided by the user is “25”.
Correct Answer: Enter a number: 25 scanf returned: 1
Explanation: The first `printf` prompts the user to enter a number. The `scanf` reads the input and returns the number of successfully read items, which is 1 in this case. The second `printf` prints this return value.
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("Hexadecimal: %x\n", num);
return 0;
}
Assume the input provided by the user is “255”.
Correct Answer: Enter a number: Hexadecimal: ff
Explanation: The first `printf` prompts the user to enter a number. The `scanf` reads the input, and the second `printf` prints the entered number in hexadecimal format (lowercase), which is `ff`.
#include <stdio.h>
int main() {
double d;
printf("Enter a double: ");
scanf("%lf", &d);
printf("You entered: %.4lf\n", d);
return 0;
}
Assume the input provided by the user is “123.456789”.
Correct Answer: Enter a double: You entered: 123.4568
Explanation: The first `printf` prompts the user to enter a double. The `scanf` reads the input, and the second `printf` prints the entered double value rounded to four decimal places, which is 123.4568.
#include <stdio.h>
int main() {
char str[20];
printf("Enter a string: ");
scanf("%19s", str);
printf("You entered: %s\n", str);
return 0;
}
Assume the input provided by the user is “HelloWorld”.
Correct Answer: Enter a string: HelloWorld You entered: HelloWorld
Explanation: The first `printf` prompts the user to enter a string. The `scanf` reads the input, and the second `printf` prints the entered string.
#include <stdio.h>
int main() {
int a = 10;
int b = 20;
printf("%d + %d = %d\n", a, b, a + b);
return 0;
}
Correct Answer: 10 + 20 = 30
Explanation: The `printf` function prints the values of `a` and `b` and their sum, which is `10 + 20 = 30`.
#include <stdio.h>
int main() {
int a = 5, b = 10, c;
c = a * b;
printf("Multiplication result: %d\n", c);
return 0;
}
Correct Answer: Multiplication result: 50
Explanation: The `printf` function prints the result of the multiplication of `a` and `b`, which is `5 * 10 = 50`.
#include <stdio.h>
int main() {
int x = 0;
if (x)
printf("x is true\n");
else
printf("x is false\n");
return 0;
}
Correct Answer: x is false
Explanation: In C, `0` is considered false. Since `x` is `0`, the else block is executed, printing “x is false”.
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("Octal: %o\n", num);
return 0;
}
Assume the input provided by the user is “16”.
Correct Answer: Enter a number: Octal: 20
Explanation: The first `printf` prompts the user to enter a number. The `scanf` reads the input, and the second `printf` prints the entered number in octal format, which is `20` in octal for the decimal number `16`.