You are on page 1of 12

2019

1.(a). Define assembler, interpreter, compiler. …………………………………………………………(3)

ANS: The source programming language is either a high level programming language or assembly
language. The target language in almost all cases is machine language which is in binary. A translator is a
program that converts source code into object code. Generally, there are three types of translator:
compilers , interpreters and assemblers.

Assembler:
1. Translate low level language into machine code.
2. Written for particular hardware.
3. One instruction translate to many instruction .(one to one).
4. Translates entire program before running.
5. They produce object code.

Compiler:
1. Translate high level language into machine language.
2. Written for particular language.
3. One instruction translates to many instruction.(one to many)
4. Translates entire program before running.

Interpreter:
1. Translate high level language to machine code.
2. Written for particular language.
3. One instruction translates to many instructions.
4. Translates program instruction by instruction until an either completed or error detected.
5. It does not produce an object code.

1.(b). What are basic data types that c supports ? What is identifier in c? …………………………………(1.75)

ANS: C supports three classes of data types:

1. Primary or fundamental data types:


integer (int), character (char), floating point (float), double-precision

floating point (double) and void

2. Derived data types:


array, function, structure and pointer

3. User-defined data types

In C, the names of variables, functions, labels, and various other user-defined items are called
identifiers. The length of these identifiers can vary from one to several characters. The first character
must be a letter or an underscore, and subsequent characters must be either letters, digits, or
underscores. Here are some correct and incorrect identifier names:
Correct Incorrect

count 1count

test23 hi!there
high_balance high . . . balance

1.(c). What is operator ? What are the different types of operators used in c programming?
…………….(2)

ANS: An operator is a symbol that tells the computer to perform certain mathematical or logical
manipulations. Operator are used in programs to manipulate data and variables. They usually
form a part of a mathematical or logical expressions.

C operators can be classified into a number of categories. They include :


1. Arithmetic operators
2. Relational operators
3. Logical operators
4. Assignment operators
5. Increment and decrement operators
6. Conditional operators
7. Bitwise operators
8. Special operators

1.(d). Evaluate the value of z from the following expression if x=19 and y=5;

i) z = y%x, ii) z = |(y-x)| ………………(2)

ANS:
#include<stdio.h>

#include<math.h>

int main()

int x = 19,y = 5, z;

z = y%x;

printf("z = %d\n",z);

z = fabs(y-x);

printf("z = %d\n",z);

return 0;}

Output of the program:

Z=5

Z = 14

−𝒃+√𝒂𝟐 −𝟒𝒂𝒄
2.(a). Convert the following expression to c expression: x= …………………………………(1)

ANS:
#include<stdio.h>
#include<conio.h>

#include<math.h>

int main()

float a, b, c, x, z;

float root_part;

printf("Enter the value of a, b and c\n");

scanf("%f %f %f",&a, &b, &c);

z = a*a - 4*a*c;

if(z>0){

root_part = sqrt(a*a - 4*a*c);

x = -b + root_part;

printf("%f",x);

else

printf("There is no real value.");

getch();

2.(b). Find how many times ‘var’ will be printed, and explain why:

#include<stdio.h>

int main()

int var=1;

While(var <= 2)

Printf(“%d”,var);

} ………………………………………………………(1)

ANS: ‘var’ will be printed infinite times. Because it is a infinite loop. The infinite loop happens when the
condition in the while loop always evaluates to true. This can happen when the variables within the loop aren't
updated correctly, or aren't updated at all. ... The condition evaluates to true, and the loop begins an infinite run.
2.(c). Write a c program to convert specified days into years, weeks and days.(ignore leap year)
…………………..(3.50)

ANS:
#include<stdio.h>

int main()

int years, weeks, days;

printf("Enter days\n");

scanf("%d", &days);

years = days/365;

weeks = (days%365)/7;

days = days-((years*365)+(weeks*7));

printf("%d years\n%d weeks \n%d days",years,weeks,days);

2.(d). Write a C program to determine whether a number is odd or even, and print the
message”NUMBER IS EVEN” or “NUMBER IS ODD” accordingly. ………………………………(3.25)

ANS:
#include<stdio.h>

#include<stdlib.h>

int main()

int number;

printf("Enter the number to check even of odd\n");

scanf("%d", &number);

if((number % 2) == 0){

printf("%d is a even number", number);

else{

printf("%d is odd number", number);

3.(a). Explain Switch statement with an example. ……………………………………………………(2.0)

ANS: Switch is a multiway decision statement. The switch statement tests the value of a given
variable(or expression) against a list of case values and when a match is found, a block of statements
associated with that case is executed. The general form of the switch statement is as shown below:
The expression is an integer expression or characters. Value-1,value-2 ……are constants or constant
expressions and are known as case levels. Each of these values should be unique within a switch
statements.

When the switch the executed, the value of the expression is successfully compared against the values
value-1,value-2… if a case is found whose value matches with the value of the expression, then the block
of statements that follows the case executed.

The break statement at the end of each block signals the end of a particular case and causes an exit from
the switch statement, transferring the control to the statement-x following the switch.

3.(b). Write a program using a do……..while loop to calculate and print the first m Fibonacci numbers.
…………………..(3.0)

ANS:
#include<stdio.h>

void main()

int num1 = 0, num2 = 1, m, i, fib;

printf("\n\n Enter the value of m:");

scanf("%d",&m);

i = 0;

do {

if(i<=1)

printf(" %d ",i);

else

{fib = num1 + num2;

printf(" %d ", fib);

num1 = num2;
num2 = fib;}

i++;

while(i<= m);

3.(c). What is the purpose of if …… else statement? …………………………………(1)

ANS: The if…………….else statement is an extension of the simple if statement. In C it is used to perform the operations based
on some specific condition. The operations specified in if block are executed if and only if the given condition is true. If the test
expression is true, then the true-block statement(s), immediately following the if statements are executed.; otherwise, the false-block
statement(s) are executed. In either case, either true-block or false-block will be executed, not both.

3.(d). Write a program to print the following output using for loops.
……………………………………….(2.75)

*****

****

***

**

ANS:
#include<stdio.h>

int main()

int i, j;

for(i=1 ; i<=5 ; i++)

for(j=5 ; j>=I ; j- -)

printf(" * ");

printf("\n");

4.(a). What is an array? Explain with example how an array can be passed to a function as an
argument. …………….(3)

ANS: An array is a sequenced collection of related data items that share a common name. It is simply a
grouping of like type data.

An array is a collection of variables of the same type that are referred to through a common name. A
specific element in an array is accessed by an index. In C, all arrays consist of contiguous memory
locations. The lowest address corresponds to the first element and the highest address to the last
element. Arrays can have from one to several dimensions. The most common array is the string, which is
simply an array of characters terminated by a null.

C does not permit to pass an entire array as an argument to a function. It permits to pass a pointer to an
array by specifying array’s name without an index. Its only pass the address of the first array not all the
arrays address. For example, the following program fragment passes the address of i to func():

int main(void)

int i[10];

func1(i); //actually starting address of array is going into the function

… … … …

… … … …

void func1(int*x) /*pointer*/

*……………………….. *

Or,

void func1(int x[]) //using array

*………………………....*

The first declaration actually uses a pointer. The second employs the standard array declaration.

4.(b). Write a program to find the average of n (n < 10) numbers using arrays.

ANS:

4.(c). What is pointer? Describe how pointer is declared and initialized? …………………………..(2)

ANS: A pointer is a variable that holds a memory address. This address is the location of another object
(typically another variable) in memory. If one variable contains the address of another variable, the first
variable is said to point to the second. Pointers can be used to access and manipulate data stored in the
memory.

Declaration: The declaration of a pointer variables takes the following form:

data_type *pt_name;

1. The asterisk(*) tells that the variable pt_name is a pointer

4.(d). What is dynamic memory allocation? Write the syntax of malloc().


………………………………………..(1.0)

ANS: The process of allocating memory at run time is known as dynamic memory allocation. Dynamic
data structure provide flexibility in adding, deleting or rearranging data items at run time. Dynamic
memory management techniques permit us to allocate additional memory space or to release
unwanted space at run time.

A block of memory may be allocated using the function malloc. The malloc function reserves a block of
memory of specified size and returns a pointer of type void. It takes the following form

x = (int*) malloc (byte-size);

SECTION-B(Answer any THREE Qquestion)

5.(a). Define function used in C. Describe the necessity of user defined function.

ANS: Function: A function is a group of statements that together perform a task.

Necessity of user defined function: User-defined functions allow programmers to create


their own routines and procedures that the computer can follow; it is the basic
building block of any program and also very important for modularity and code
reuse since a programmer could create a user-defined function which does a
specific process and simply call it.
5.(b). Categorize the function in C. Briefly explain each of them.

ANS: There are two types of function in C programming:

• Standard library functions.


• User-defined functions.
Library function: : Library functions are built-in functions in a programming language, that are
grouped together and placed in a common location called library.
User-defined function: A user-defined function is a function provided by the user of a program or
environment, in a context where the usual assumption is that functions are built into the program
or environment.

5.(c). What is recursion? Write a program to calculate the factorial of a number ‘n’.

ANS: Recursion is the process which comes into existence when a function calls a copy of itself to
work on a smaller problem.
#include<stdio.h>

long int multiplyNumbers(int n);


int main() {
int n;
printf("Enter a positive integer: ");
scanf("%d",&n);
printf("Factorial of %d = %ld", n, multiplyNumbers(n));
return 0;
}

long int multiplyNumbers(int n) {


if (n>=1)
return n*multiplyNumbers(n-1);
else
return 1;
}

6.(a). Define string. What are the common operations performed on string?

ANS: String: In computer programming, a string is traditionally a sequence of characters, either as a


literal constant or as some kind of variable.
Common operations performed on string:

strcmp() Compares two strings in a case-sensitive way. If the strings


match, the function returns 0.
strncmp() Compares the first n characters of two strings, returning 0 if
the given number of characters match.

strcat() Appends one string to another, creating a single string out of


two.

strncat() Appends a given number of characters from one string to the end
of another.

6.(b). Explain how to declare and initialize string variable.

ANS: char str_name[size];


Str_name = “string value”
6.(c). Explain reading and writing operations on string.

ANS: #include <stdio.h>

int main () {
char str[50];

printf("Enter a string : ");


gets(str); //reading string

printf("You entered: %s", str); //print string

return(0);
}

6.(d). Explain strcmp() function.

ANS: The strcmp() compares two strings character by character. If the strings are equal, the function
returns 0.

7.(a). What is structure ? How does structure differ from array?

ANS: Structure: In C programming, a structure is a collection of variables (can be of different types)


under a single name.

A structure creates a data type that can be used to group items of possibly different
types into a single type. But Array refers to a collection consisting of elements of homogeneous
data type.
7.(b). How can you initialize a structure?
ANS: struct Point

int x, y;

};

int main()

struct Point p1; // The variable p1 is declared like a normal variable

p1.x = 10;

p1.y = 20;

return 0;

7.(c). What you mean by nesting of structure?

ANS: A structure inside another structure is called nested structure.

Example:

stuct emp{

int a;

char name[30];

struct allowance{

int num;

char address[30];

}a;

}e;

7.(d). Define union.


ANS: Union: Union is an user defined data type in C programming language. It is a collection of variables
of different data types in the same memory location

8.(a). What is object oriented programming ? Write down the features of object oriented
programming.

ANS: Object-oriented programming is a programming paradigm based on the concept of "objects",


which can contain data and code: data in the form of fields, and code, in the form of procedures.

There are three major features in object-oriented programming:

• Encapsulation
• Inheritance
• Polymorphism.

8.(b). What are mean by function overloading and operator overloading?

ANS: Function overloading: Function overloading is a feature of object oriented


programming where two or more functions can have the same name but different
parameters.
Operator Overloading: Operator overloading is a feature of object oriented programming
where two or more operator can have the same name but different datatype.
8.(c). Explain with an appropriate example, the prefix ++increment operator overloading(no return
type).

ANS:

You might also like