Professional Documents
Culture Documents
22POP13set2 Answers
22POP13set2 Answers
Model Question Paper-II with effect from 2022-23 (CBCS Scheme) - 22POP13
printf( ): In C programming language, printf() function is used to print the “character, string, float,
integer, octal and hexadecimal values” onto the output screen. The features of printf() can be
effectively exploited to control the alignment and spacing of printouts on terminals.
#include <stdio.h>
int main()
{ // Declaring an int type variable
int a;
// Assigning a value in a variable
a = 20;
// Printing the value of a variable
printf("%d", a);
return 0;
}
o Syntax error
o Run-time error
o Linker error
o Logical error
o Semantic error
o Syntax error
Syntax errors
Syntax errors are also known as the compilation errors as they occurred at the compilation
time, or we can say that the syntax errors are thrown by the compilers. These errors are
mainly occurred due to the mistakes while typing or do not follow the syntax of the
specified programming language. These mistakes are generally made by beginners only
because they are new to the language. These errors can be easily debugged or corrected.
Run-time error
Sometimes the errors exist during the execution-time even after the successful compilation
known as run-time errors. When the program is running, and it is not able to perform the
operation is the main cause of the run-time error. The division by zero is the common example
of the run-time error. These errors are very difficult to find, as the compiler does not point to
these errors.
Linker error
Linker errors are mainly generated when the executable file of the program is not created. This
can be happened either due to the wrong function prototyping or usage of the wrong header
file. For example, the main.c file contains the sub() function whose declaration and definition
is done in some other file such as func.c. During the compilation, the compiler finds
the sub() function in func.c file, so it generates two object files, i.e., main.o and func.o. At the
execution time, if the definition of sub() function is not found in the func.o file, then the linker
error will be thrown. The most common linker error that occurs is that we use Main() instead
of main().
Logical error
The logical error is an error that leads to an undesired output. These errors produce the incorrect
output, but they are error-free, known as logical errors. These types of mistakes are mainly
done by beginners. The occurrence of these errors mainly depends upon the logical thinking of
the developer. If the programmers sound logically good, then there will be fewer chances of
these errors.
Semantic error
Semantic errors are the errors that occurred when the statements are not understandable by the
compiler.
2a. Explain the various rules for forming identifiers names. Give examples for valid and invalid
identifiers for the same
1. The first character of the identifier must always be a letter or an underscore followed by any number
of letters digits or underscore.
5. Identifiers are case sensitive (A same variable name declared in uppercase letters and lower case
letters are two different variables in C program)
1. The Monitor
2. Printer
3. Monitor
4. Speaker
hardcopy device:
Impact Printers: An impact printer is a type of printer that operates by striking a metal or plastic head
against an ink ribbon. The ink ribbon is pressed against the paper, marking the page with the
appropriate character , dot, line, or symbol.
Dot matrix Printer: A dot matrix printer (DMP) is a type of printer which uses pins impacting an ink
ribbon to print. These printers are generally considered outdated, as they cannot create high-quality
prints and are costly as well. they can be used to print multiple copies of text at the same time with
the help of carbon copying.
Daisy Wheel Printer: A type of printer that produces letter-quality type. A daisy-wheel printer works
on the same principle as a ball-head typewriter. The daisy wheel is a disk made of plastic or metal on
which characters stand out in relief along the outer edge. To print a character, the printer rotates the
disk until the desired letter is facing the paper. Then a hammer strikes the disk, forcing the character
to hit an ink ribbon, leaving an impression of the character on the paper.
Line Printer: A line printer is an impact printer which makes use of a continuous feed of paper and
prints one line of text at a time. A line printer is also known as a bar printer. High speed is one of the
advantages of line printers.
Laser Printer: The printers laser beams your print onto a metal cylinder called a drum. Using static
electricity, the drum attracts powdered toner from its cartridge to the drum. The drum rolls the toner
onto the paper in the form of your print. The toner is melted onto the paper by heat from a fuser as it
passes underneath. Your print comes out of the printer
Plotters: A plotter is a printer designed for printing vector graphics. Instead of printing individual dots
on the paper, plotters draw continuous lines. This makes plotters ideal for printing architectural
blueprints, engineering designs, and other CAD drawings. There are two main types of plotters – drum
and flatbed plotters.
2c. Discuss the variants of microcomputer that are widely used today
1. Desktop Microcomputer
The desktop microcomputer plays an important role in the users’ life because it allows them to complete
several complex tasks in a short period of time without experiencing any difficulties. This computer sits
on a table and is connected to numerous components through wires, including a monitor, keyboard,
system unit, and mouse. Users can use these components to insert their commands and receive output
based on the input instructions. In addition, these computers can connect to the internet through Wi-Fi
or a LAN cable. However, it has some disadvantages, such as a larger footprint than most Desktop
Microcomputers (Laptops, PDA, smartphones, and Notebook). In addition, it isn’t portable since it has
a set location.
2. Microcomputer Laptop
A laptop, also known as a “Notebook,” is a computer that looks like a sleek briefcase. They can also
handle more sophisticated tasks, such as a desktop computer, and they can run on their own in-built
battery and a wall socket. Since different small-size components, including a keyboard, LCD monitor,
touchpad, and other internal parts, are incorporated into these microcomputers, they are more costly
than desktop computers (CPU, Motherboard, Hard disk, etc.). However, its greatest benefit is its
portability.
Tablet microcomputers are compact mobile computers with touch screen displays that are smaller than
laptop computers but larger than smartphones. On the LCD panel, users can perform both input and
output operations. In addition, they can access the internet through Wi-Fi and other cellular networks.
Users can experience issues due to a lack of a keyboard, but they may use an external keyboard if more
is needed. Tablets such as Apple’s iPad, Microsoft’s Surface, and Amazon’s Kindle Fire are
commercially available.
Smartphones
A smartphone is a cell phone with a touch screen that can handle advanced tasks close to those of a
computer, such as installing an operating system, downloading software, accessing the internet, and
more.
The bitwise operators are the operators used to perform the operations on the data at the bit-
level. When we perform the bitwise operations, then it is also known as bit-level programming.
It consists of two digits, either 0 or 1. It is mainly used in numerical computations to make the
calculations faster.
We have different types of bitwise operators in the C programming language. The following is
the list of the bitwise operators:
| Bitwise OR operator
Syntax :
Jump-statement;
break;
continue statement: The continue statement is used to bypass the remainder of the current pass through
a loop.
The loop does not terminate when a continue statement is encountered.
Instead, the remaining loop statements are skipped and the computation proceeds directly to the next
pass through the loop.
It is simply written as “continue”. The continue statement tells the compiler “Skip the following
Statements and continue with the next Iteration”.
Syntax :
Jump-statement;
Continue;
#include <stdio.h>
int main() {
int n, n1, rev = 0, rem;
//logic
while (n > 0){
rem = n % 10;
rev = rev * 10 + rem;
n = n / 10;
}
if (n1 == rev){
printf("Given number is a palindromic number");
}
else{
printf("Given number is not a palindromic number");
}
return 0;
}
4c. Explain switch statement with syntax. Write a C program to simulate calculator
A switch statement tests the value of a variable and compares it with multiple cases. Once the case
match is found, a block of statements associated with that particular case is executed. Each case in a
block of a switch has a different name/number which is referred to as an identifier. The value provided
by the user is compared with all the cases inside the switch block until the match is found. If a case
match is not found, then the default statement is executed, and the control goes out of the switch
block. The break statement is used at the end of each case to come out of the switch block.
• The recursive function must always have a stopping condition or an exit condition in the function
else the program will go into infinite loop
Local variables
Local variables are the ones that are declared inside the function or the block.
Only the statements which are inside this block are able to access these variables.
They are not known to the functions outside.
void main()
{
int x,y;
int c;
}
Global variables
The variables which are defined outside a function or block generally on top of the
program are known as Global variables.
The value of the variable is held throughout the program.
They can be accessed anywhere in the program and in any function.
int n;
int main()
{
code;
}
Formal parameters: They are taken as local variables within the function and take a
precedence over the global variables.
6a. Differentiate between call by value and call by reference. Using suitable example
7a. Mention various operations that can be performed on string using built-in functions. Explain any
two function.
String constant or a string literal can be defined as a sequence of characters enclosed in double quotes
that will be treated as a single data element followed by a null character ‘\0’ (Null character indicates
the end of string).
various operations:
7b. Develop a program using pointer to compute the sum, mean and standard deviation of all
element stored in array of N real number.
1. By char array
2. By string literal
char ch[10]={'j', 'a', 'v', 'a', 't', 'p', 'o', 'i', 'n', 't', '\0'};
if we directly specify the entire string at a time we use “ ” • if we specify the characters separately
we should use ‘ ’, for each character and finally enclosing all the characters within { }
8a. Write a program to compare two strings without using built-in function
• Every variable is a memory location and every memory location has its address defined which
can be accessed using ampersand (&) operator, which denotes an address in memory.
Arithmetic Pointer
• A pointer in c is an address, which is a numeric value. Therefore, you can perform arithmetic
operations on a pointer.
• It is a method of calculating the address of an object with the help of arithmetic
operations on pointers and use of pointers in comparison operations.
• Pointer values can be added or subtracted. Two pointers cannot be multiplied or divided
directly.
puts(s);
return 0;
}
9a. Explain various modes in which file can be opened for processing
9b. Implement structure to read, write and compute average marks of the students. List the
students scoring above and below the average marks for a class of n students.
• Enumeration or Enum in C is a special kind of data type defined by the user. It consists
of constant integrals or integers that are given names by a user.
• The use of enum in C to name the integer values
#include <stdio.h>
Saturday};
void main(){
for(int i=Sunday;i<=Saturday;i++){
printf("%d, ",i);
}
}
10a. Write a short note on functions used to Read data from a file Write data to a file
Writing a File
The function fputc() writes the character value of the argument c to the output stream
referenced by fp. It returns the written character written on success otherwise EOF if there is
an error. You can use the following functions to write a null-terminated string to a stream −
The function fputs() writes the string s to the output stream referenced by fp. It returns a non-
negative value on success, otherwise EOF is returned in case of any error. You can use int
fprintf(FILE *fp,const char *format, ...) function as well to write a string into a file. Try the
following example.
#include <stdio.h>
main() {
FILE *fp;
fp = fopen("/tmp/test.txt", "w+");
fprintf(fp, "This is testing for fprintf...\n");
fputs("This is testing for fputs...\n", fp);
fclose(fp);
}
Reading a File: Given below is the simplest function to read a single character from a file −
int fgetc( FILE * fp );
The fgetc() function reads a character from the input file referenced by fp. The return value is
the character read, or in case of any error, it returns EOF. The following function allows to
read a string from a stream −
The functions fgets() reads up to n-1 characters from the input stream referenced by fp. It
copies the read string into the buffer buf, appending a null character to terminate the string.
If this function encounters a newline character '\n' or the end of the file EOF before they have
read the maximum number of characters, then it returns only the characters read up to that point
including the new line character. You can also use int fscanf(FILE *fp, const char *format,
...) function to read strings from a file, but it stops reading after encountering the first space
character.
#include <stdio.h>
main() {
FILE *fp;
char buff[255];
fp = fopen("/tmp/test.txt", "r");
fscanf(fp, "%s", buff);
printf("1 : %s\n", buff );
fgets(buff, 255, (FILE*)fp);
printf("2: %s\n", buff );
fgets(buff, 255, (FILE*)fp);
printf("3: %s\n", buff );
fclose(fp);
}
Structure Union
2. getc()
It reads a single character from the input and return an integer value. If it fails, it returns EOF.