Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

22POP13/23 Principles of Programming using C

Model Question Paper-II with effect from 2022-23 (CBCS Scheme) - 22POP13

Principles of Programming Using C


1a. Explain the structure of C program in detail. Write a sample program to demonstrate the
components in the structure of C program

Ms. Amrutha Bhat, Dept of CSE, MITE, Moodabidri pg. 1


22POP13/23 Principles of Programming using C

1b. Demonstrate formatted output of integer in C with suitable example.

Formatted Output statements

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.

Syntax: printf(“%d”, number);

// C program to implement printf() function

#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;
}

1c. Discuss different types of error occur in program

There are mainly five types of errors exist in C programming:

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.

Ms. Amrutha Bhat, Dept of CSE, MITE, Moodabidri pg. 2


22POP13/23 Principles of Programming using C

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

Rules to define an Identifiers

1. The first character of the identifier must always be a letter or an underscore followed by any number
of letters digits or underscore.

2. Keywords cannot be used as identifiers or variables.

3. An Identifier or a variable should not contain two consecutive underscores

4. Whitespaces and special symbols cannot be used to name the identifiers.

5. Identifiers are case sensitive (A same variable name declared in uppercase letters and lower case
letters are two different variables in C program)

Ms. Amrutha Bhat, Dept of CSE, MITE, Moodabidri pg. 3


22POP13/23 Principles of Programming using C

2b. Mention various output devices and explain hardcopy device

Output devices are:

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

Ms. Amrutha Bhat, Dept of CSE, MITE, Moodabidri pg. 4


22POP13/23 Principles of Programming using C

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.

3. Tablets and Smartphones


Tablets

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.

3a. Demonstrate the functioning of Bitwise operator in C

Bitwise operator works on bits and perform bit-by-bit operation.

The truth tables for &, |, and ^.

Ms. Amrutha Bhat, Dept of CSE, MITE, Moodabidri pg. 5


22POP13/23 Principles of Programming using C

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 AND operator

| Bitwise OR operator

^ Bitwise exclusive OR operator

~ One's complement operator (unary


operator)

<< Left shift operator

>> Right shift operator

3b. Write a C program to find roots of quadratic equation

3c. Distinguish between the break and continue statement


break Statement: A break statement terminates the execution of the loop and the control is transferred
to the statement immediately following the loop. i.e., the break statement is used to terminate loops or
to exit from a switch.

Ms. Amrutha Bhat, Dept of CSE, MITE, Moodabidri pg. 6


22POP13/23 Principles of Programming using C

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;

4a. Illustrate Nested loops in C with suitable example


• Nested loop means a loop statement inside another loop statement. That is why nested loops
are also called as “loop inside loop“.
• In nested for loop one or more statements can be included in the body of the loop.
• In nested for loop, The number of iterations will be equal to the number of iterations in the
outer loop multiplies by the number of iterations in the inner loop.
• When the control moves from outer loop to inner loop the control remains in the inner loop
until the inner loop condition fails, once the condition fails the control continues with the outer
loop condition Again when the control comes to inner loop the inner loop is reset to the initial
value.
• The Nested for loop stops execution when the outer for loop condition fails

Ms. Amrutha Bhat, Dept of CSE, MITE, Moodabidri pg. 7


22POP13/23 Principles of Programming using C

4b. Write a C program to print whether a given number is palindrome or not

#include <stdio.h>

int main() {
int n, n1, rev = 0, rem;

printf("Enter any number: ");


scanf("%d", &n);
n1 = n;

//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

Ms. Amrutha Bhat, Dept of CSE, MITE, Moodabidri pg. 8


22POP13/23 Principles of Programming using C

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.

Ms. Amrutha Bhat, Dept of CSE, MITE, Moodabidri pg. 9


22POP13/23 Principles of Programming using C

5a. Write a C program to implement Bubble sort technique(ascending order)

5b. Illustrate the concept of recursive function with example

when a function calls itself again and again it is called as recursion

• Here the calling function and called function are same

• It is a powerful technique of writing complex algorithms in an easier way

• The recursive function must always have a stopping condition or an exit condition in the function
else the program will go into infinite loop

Ms. Amrutha Bhat, Dept of CSE, MITE, Moodabidri pg. 10


22POP13/23 Principles of Programming using C

5c. Discuss various scope of variables


The three places where the variables can be declared are:

1. Local variables which are inside a function or a block.


2. Global variables which are outside all the functions.
3. Formal parameters which are defined in the function parameters.

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.

Ms. Amrutha Bhat, Dept of CSE, MITE, Moodabidri pg. 11


22POP13/23 Principles of Programming using C

6a. Differentiate between call by value and call by reference. Using suitable example

Ms. Amrutha Bhat, Dept of CSE, MITE, Moodabidri pg. 12


22POP13/23 Principles of Programming using C

6b. Write a C program to transpose a MxN matrix

6c. Discuss the various storage classes

Ms. Amrutha Bhat, Dept of CSE, MITE, Moodabidri pg. 13


22POP13/23 Principles of Programming using C

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:

Ms. Amrutha Bhat, Dept of CSE, MITE, Moodabidri pg. 14


22POP13/23 Principles of Programming using C

7b. Develop a program using pointer to compute the sum, mean and standard deviation of all
element stored in array of N real number.

Ms. Amrutha Bhat, Dept of CSE, MITE, Moodabidri pg. 15


22POP13/23 Principles of Programming using C

7c. Explain how strings are represented in main memory

The string can be defined as the one-dimensional array of characters terminated by a


null ('\0'). The character array or the string is used to manipulate text such as word or
sentences. Each character in the array occupies one byte of memory, and the last
character must always be 0. The termination character ('\0') is important in a string
since it is the only way to identify where the string ends. When we define a string as
char s[10], the character s[10] is implicitly initialized with the null in the memory.

There are two ways to declare a string in c language.

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

8b. What is pointer? Discuss pointer arithmetic with suitable C code


• The pointers in C language refer to the variables that hold the addresses of different
variables of similar data types.
• We use pointers to access the memory of the said variable and then manipulate their
addresses in a program.

Ms. Amrutha Bhat, Dept of CSE, MITE, Moodabidri pg. 16


22POP13/23 Principles of Programming using C

• 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.

8c. Explain gets()and puts() function with example


The gets function helps to obtain a string from a standard input device such as a keyboard.
The puts function helps to display a string to the standard output device such as a monitor. It
appends a newline character to the output.
get string: gets()
char varname[ ];
gets(varname);
put string: puts()
char varname[ ];
puts(varname);
#include<stdio.h>
int main()
{
char s[30];
printf("Enter the string? ");
gets(s);
printf("You entered a string called:");

Ms. Amrutha Bhat, Dept of CSE, MITE, Moodabidri pg. 17


22POP13/23 Principles of Programming using C

puts(s);
return 0;
}

9a. Explain various modes in which file can be opened for processing

The fopen() function is used to create a file or open an existing file:


fp = fopen(filename, mode);
fp=fopen(“C:\\Desktop\a.txt”, w);
There are many modes for opening a file:

 r - open a file in read mode


 w - opens or create a text file in write mode
 a - opens a file in append mode
 r+ - opens a file in both read and write mode
 a+ - opens a file in both read and write mode
 w+ - opens a file in both read and write mode

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.

Ms. Amrutha Bhat, Dept of CSE, MITE, Moodabidri pg. 18


22POP13/23 Principles of Programming using C

9c. What are enumeration variable? How are they declared.

Ms. Amrutha Bhat, Dept of CSE, MITE, Moodabidri pg. 19


22POP13/23 Principles of Programming using C

• 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

• enum condition (true, false); //declaring the enum

• enum condition e; //creating a variable of type condition

#include <stdio.h>

enum days{Sunday=1, Monday, Tuesday, Wednesday, Thursday, Friday,

Saturday};

void main(){

// printing the values of weekdays

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

Following is the simplest function to write individual characters to a stream −

int fputc( int c, FILE *fp );

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 −

int fputs( const char *s, FILE *fp );

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.

Ms. Amrutha Bhat, Dept of CSE, MITE, Moodabidri pg. 20


22POP13/23 Principles of Programming using C

#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 −

char *fgets( char *buf, int n, FILE *fp );

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);
}

Ms. Amrutha Bhat, Dept of CSE, MITE, Moodabidri pg. 21


22POP13/23 Principles of Programming using C

10b. Differentiate structures and unions with syntax and example

Structure Union

Structure is a collection of one or more A union is a special data type available in


variables of same or different data types C that allows to store different data types in
grouped to gather under a single name for the same memory location.
easy handling.

10c. How to detect END-OF-FILE


The End of the File (EOF) indicates the end of input.
After we enter the text, if we press ctrl+Z, the text terminates i.e. it indicates the file reached
end nothing to read.
Two methods to detect EOF
1. The function feof() is used to check the end of file after EOF. It tests the end of file indicator.
It returns non-zero value if successful otherwise, zero

Ms. Amrutha Bhat, Dept of CSE, MITE, Moodabidri pg. 22


22POP13/23 Principles of Programming using C

2. getc()

It reads a single character from the input and return an integer value. If it fails, it returns EOF.

Ms. Amrutha Bhat, Dept of CSE, MITE, Moodabidri pg. 23

You might also like