AKTU 1st Year Sem 2 Solved Paper 2015-16 | COMP. SYSTEM & C PROGRAMMING | Sec B
Paper download link: Paper | Sem 2 | 2015-16
B.Tech.
(SEM-II) THEORY EXAMINATION 2015-16
COMPUTER SYSTEM & PROGRAMMING IN C
Time: 3hrs
Total Marks: 100
Note:-
- There are three sections. Section A carries 20 marks, Section B carries 50 marks and Section C carries 30 marks.
- Attempt all questions. Marks are indicated against each question.
- Assume suitable data wherever necessary.
Section – B
2. Attempt any five parts: (10*5 = 50)
- What is special about void pointer?
A void pointer is a pointer that has no associated data type with it. A void pointer can hold address of any type and can be typcasted to any type.inta = 10;charb ='x';void* p = &a;// void pointer holds address of int 'a'p = &b;// void pointer holds address of char 'b'chevron_rightfilter_noneAdvantages of void pointers:
1) malloc() and calloc() return void * type and this allows these functions to be used to allocate memory of any data type (just because of void *)int main(void){// Note that malloc() returns void * which can be// typecasted to any type like int *, char *, ..int* x = malloc(sizeof(int) * n);}chevron_rightfilter_noneNote that the above program compiles in C, but doesn’t compile in C++. In C++, we must explicitly typecast return value of malloc to (int *).
2) void pointers in C are used to implement generic functions in C. For example compare function which is used in qsort().
Some Interesting Facts:
1) void pointers cannot be dereferenced. For example the following program doesn’t compile.#include <stdio.h>intmain(){inta = 10;void* ptr = &a;printf("%d", *ptr);return0;}chevron_rightfilter_noneOutput:
Compiler Error: 'void*' is not a pointer-to-object type
The following program compiles and runs fine.
#include <stdio.h>intmain(){inta = 10;void* ptr = &a;printf("%d", *(int*)ptr);return0;}chevron_rightfilter_noneOutput:
10
2) The C standard doesn’t allow pointer arithmetic with void pointers. However, in GNU C it is allowed by considering the size of void is 1. For example the following program compiles and runs fine in gcc.
#include <stdio.h>intmain(){inta[2] = { 1, 2 };void* ptr = &a;ptr = ptr +sizeof(int);printf("%d", *(int*)ptr);return0;}chevron_rightfilter_noneOutput:
2
Note that the above program may not work in other compilers.
- What do you mean by parameter passing mechanism?
There are different ways in which parameter data can be passed into and out of methods and functions. Let us assume that a function B() is called from another function A(). In this case A is called the “caller function” and B is called the “called function or callee function”. Also, the arguments which A sends to B are called actual arguments and the parameters of B are called formal arguments.Important methods of Parameter Passing
- Pass By Value : This method uses in-mode semantics. Changes made to formal parameter do not get transmitted back to the caller. Any modifications to the formal parameter variable inside the called function or method affect only the separate storage location and will not be reflected in the actual parameter in the calling environment. This method is also called as call by value.

// C program to illustrate// call by value#include <stdio.h>voidfunc(inta,intb){a += b;printf("In func, a = %d b = %d\n", a, b);}intmain(void){intx = 5, y = 7;// Passing parametersfunc(x, y);printf("In main, x = %d y = %d\n", x, y);return0;}chevron_rightfilter_noneOutput:
In func, a = 12 b = 7 In main, x = 5 y = 7
Languages like C, C++, Java support this type of parameter passing. Java in fact is strictly call by value.
Shortcomings:- Inefficiency in storage allocation
- For objects and arrays, the copy semantics are costly
- Pass by reference(aliasing) : This technique uses in/out-mode semantics. Changes made to formal parameter do get transmitted back to the caller through parameter passing. Any changes to the formal parameter are reflected in the actual parameter in the calling environment as formal parameter receives a reference (or pointer) to the actual data. This method is also called as <em>call by reference. This method is efficient in both time and space.

// C program to illustrate// call by reference#include <stdio.h>voidswapnum(int* i,int* j){inttemp = *i;*i = *j;*j = temp;}intmain(void){inta = 10, b = 20;// passing parametersswapnum(&a;, &b;);printf("a is %d and b is %d\n", a, b);return0;}chevron_rightfilter_noneOutput:
a is 20 and b is 10
- Pass By Value : This method uses in-mode semantics. Changes made to formal parameter do not get transmitted back to the caller. Any modifications to the formal parameter variable inside the called function or method affect only the separate storage location and will not be reflected in the actual parameter in the calling environment. This method is also called as call by value.
- Write a program in C to print the following pattern:
ABCDEFGFEDCBA ABCDEF FEDCBA ABCDE EDCBA ABCD DCBA ABC CBA AB BA A A
#include <stdio.h>intmain(){charch ='A', ch1;inti, j, n = 7;for(inti = 0; i < n; i++) {for(intj ='A'; j <'A'+ (2 * n) - 1; j++) {if(j >= ('A'+ n - 1) + i)printf("%c",(char)(('A'+ n - 1)- (j % ('A'+ n - 1))));elseif(j <= ('A'+ n - 1) - i)printf("%c", (char)j);elseprintf(" ");}printf("\n");}return0;}chevron_rightfilter_noneOutput:
ABCDEF FEDCBA ABCDE EDCBA ABCD DCBA ABC CBA AB BA A A
- Write a program to check whether the given character is in upper case, lower case or non alphabetic character.
#include <stdio.h>// Driver Codeintmain(){charch;intascii;// Get the characterscanf("%c", &ch;);// Get ASCII value of the characterascii = ch;if(ascii >= 65 && ascii <= 91)printf("%c is an UpperCase character", ch);elseif(ascii >= 97 && ascii <= 122)printf("%c is an LowerCase character", ch);elseprintf("%c is not an aplhabetic character", ch);return0;}chevron_rightfilter_noneOutput:
A is an UpperCase character a is an LowerCase character 0 is not an aplhabetic character
- What are the disadvantages of if-else-if ladder?
if-else-if ladderHere, a user can decide among multiple options. The if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the ladder is bypassed. If none of the conditions is true, then the final else statement will be executed.
Syntax:
if (condition) statement; else if (condition) statement; . . else statement;
Example:
// C++ program to illustrate if-else-if ladder#include <iostream>usingnamespacestd;intmain(){inti = 20;if(i == 10)cout <<"i is 10";elseif(i == 15)cout <<"i is 15";elseif(i == 20)cout <<"i is 20";elsecout <<"i is not present";}chevron_rightfilter_noneOutput:
i is 20
Disadvantage:
- The if-else-if ladder are hard to understand and modify.
- As the length of if-else-if ladder increases, the readability of the program decreases.
- In situations involving a series of decisions one after the other, each decision or condition may involve expressions which result in integer value. In these situations, the usage of the if-else-if ladder is not recommended. Instead of this statement, we go for the switch statement.
- What are the principles of recursion? Explain in detail.
The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called as recursive function. Using recursive algorithm, certain problems can be solved quite easily. Examples of such problems are Towers of Hanoi (TOH), Inorder/Preorder/Postorder Tree Traversals, DFS of Graph, etc.What is base condition in recursion?
In recursive program, the solution to base case is provided and solution of bigger problem is expressed in terms of smaller problems.int fact(int n) { if (n < = 1) // base case return 1; else return n*fact(n-1); }In the above example, base case for n < = 1 is defined and larger value of number can be solved by converting to smaller one till base case is reached.
How a particular problem is solved using recursion?
The idea is represent a problem in terms of one or more smaller problems, and add one or more base conditions that stop recursion. For example, we compute factorial n if we know factorial of (n-1). Base case for factorial would be n = 0. We return 1 when n = 0.Program to find factorial of a number:
// C program to find factorial of given number#include <stdio.h>// function to find factorial of given numberunsignedintfactorial(unsignedintn){// Base caseif(n == 0)return1;// Recursively call factorial functionreturnn * factorial(n - 1);}intmain(){intnum;// Ge the number of which// factorial is to be calculatedscanf("%d", #);printf("Enter the number: %d", num);// Find the factorial// and print the resultprintf("\nFactorial of %d is %d",num, factorial(num));return0;}chevron_rightfilter_noneOutput:
Enter the number: 5 Factorial of 5 is 120
- Describe the relation between structures and pointers
Self Referential structures are those structures that have one or more pointers which point to the same type of structure, as their member.
In other words, structures pointing to the same type of structures are self-referential in nature.
Example:
structnode {intdata1;chardata2;structnode* link;};intmain(){structnode ob;return0;}chevron_rightfilter_noneIn the above example ‘link’ is a pointer to a structure of type ‘node’. Hence, the structure ‘node’ is a self-referential structure with ‘link’ as the referencing pointer.
An important point to consider is that the pointer should be initialized properly before accessing, as by default it contains garbage value.Types of Self Referential Structures
- Self Referential Structure with Single Link
- Self Referential Structure with Multiple Links
Self Referential Structure with Single Link: These structures can have only one self-pointer as their member. The following example will show us how to connect the objects of a self-referential structure with the single link and access the corresponding data members. The connection formed is shown in the following figure.

#include <stdio.h>structnode {intdata1;chardata2;structnode* link;};intmain(){structnode ob1;// Node1// Intializationob1.link = NULL;ob1.data1 = 10;ob1.data2 = 20;structnode ob2;// Node2// Initializationob2.link = NULL;ob2.data1 = 30;ob2.data2 = 40;// Linking ob1 and ob2ob1.link = &ob2;// Accessing data members of ob2 using ob1printf("%d", ob1.link->data1);printf("\n%d", ob1.link->data2);return0;}chevron_rightfilter_noneOutput:30 40
Self Referential Structure with Multiple Links: Self referential structures with multiple links can have more than one self-pointers. Many complicated data structures can be easily constructed using these structures. Such structures can easily connect to more than one nodes at a time. The following example shows one such structure with more than one links.The connections made in the above example can be understood using the following figure.

#include <stdio.h>structnode {intdata;structnode* prev_link;structnode* next_link;};intmain(){structnode ob1;// Node1// Intializationob1.prev_link = NULL;ob1.next_link = NULL;ob1.data = 10;structnode ob2;// Node2// Intializationob2.prev_link = NULL;ob2.next_link = NULL;ob2.data = 20;structnode ob3;// Node3// Intializationob3.prev_link = NULL;ob3.next_link = NULL;ob3.data = 30;// Forward linksob1.next_link = &ob2;ob2.next_link = &ob3;// Backward linksob2.prev_link = &ob1;ob3.prev_link = &ob2;// Accessing data of ob1, ob2 and ob3 by ob1printf("%d\t", ob1.data);printf("%d\t", ob1.next_link->data);printf("%d\n", ob1.next_link->next_link->data);// Accessing data of ob1, ob2 and ob3 by ob2printf("%d\t", ob2.prev_link->data);printf("%d\t", ob2.data);printf("%d\n", ob2.next_link->data);// Accessing data of ob1, ob2 and ob3 by ob3printf("%d\t", ob3.prev_link->prev_link->data);printf("%d\t", ob3.prev_link->data);printf("%d", ob3.data);return0;}chevron_rightfilter_noneOutput:10 20 30 10 20 30 10 20 30
In the above example we can see that ‘ob1’, ‘ob2’ and ‘ob3’ are three objects of the self referential structure ‘node’. And they are connected using their links in such a way that any of them can easily access each other’s data. This is the beauty of the self referential structures. The connections can be manipulated according to the requirements of the programmer.
Applications:
Self referential structures are very useful in creation of other complex data structures like: - What are the enumerated data type? Explain in detail.
Enumerated Data-type: Enumeration (or enum) is a user defined data type in C. It is mainly used to assign names to integral constants, the names make a program easy to read and maintain.
enum State {Working = 1, Failed = 0};The keyword ‘enum’ is used to declare new enumeration types in C and C++.
Following is an example of enum declaration.
// The name of enumeration is "flag" and the constant // are the values of the flag. By default, the values // of the constants are as follows: // constant1 = 0, constant2 = 1, constant3 = 2 and // so on. enum flag{constant1, constant2, constant3, ....... };Variables of type enum can also be defined. They can be defined in two ways:
// In both of the below cases, "day" is // defined as the variable of type week. enum week{Mon, Tue, Wed}; enum week day; // Or enum week{Mon, Tue, Wed}day;// An example program to demonstrate working// of enum in C#include <stdio.h>enumweek { Mon,Tue,Wed,Thur,Fri,Sat,Sun };intmain(){enumweek day;day = Wed;printf("%d", day);return0;}chevron_rightfilter_noneOutput:
2
In the above example, we declared “day” as the variable and the value of “Wed” is allocated to day, which is 2. So as a result, 2 is printed.
Another example of enumeration is:
// Another example program to demonstrate working// of enum in C#include <stdio.h>enumyear { Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec };intmain(){inti;for(i = Jan; i <= Dec; i++)printf("%d ", i);return0;}chevron_rightfilter_noneOutput:
0 1 2 3 4 5 6 7 8 9 10 11
In this example, the for loop will run from i = 0 to i = 11, as initially the value of i is Jan which is 0 and the value of Dec is 11.
Recommended Posts:
- AKTU 1st Year Sem 2 Solved Paper 2015-16 | COMP. SYSTEM & C PROGRAMMING | Sec C
- AKTU 1st Year Sem 1 Solved Paper 2016-17 | COMP. SYSTEM & C PROGRAMMING | Sec A
- AKTU 1st Year Sem 1 Solved Paper 2016-17 | COMP. SYSTEM & C PROGRAMMING | Sec B
- AKTU 1st Year Sem 2 Solved Paper 2016-17 | COMP. SYSTEM & C PROGRAMMING | Sec A
- AKTU 1st Year Sem 2 Solved Paper 2016-17 | COMP. SYSTEM & C PROGRAMMING | Sec B
- AKTU 1st Year Sem 2 Solved Paper 2017-18 | COMP. SYSTEM & C PROGRAMMING | Sec A
- AKTU 1st Year Sem 2 Solved Paper 2017-18 | COMP. SYSTEM & C PROGRAMMING | Sec B
- AKTU 1st Year Sem 2 Solved Paper 2016-17 | COMP. SYSTEM & C PROGRAMMING | Sec C
- AKTU 1st Year Sem 1 Solved Paper 2017-18 | COMP. SYSTEM & C PROGRAMMING | Sec A
- AKTU 1st Year Sem 1 Solved Paper 2017-18 | COMP. SYSTEM & C PROGRAMMING | Sec B
- AKTU 1st Year Sem 1 Solved Paper 2017-18 | COMP. SYSTEM & C PROGRAMMING | Sec C
- AKTU 1st Year Sem 2 Solved Paper 2015-16 | COMP. SYSTEM & C PROGRAMMING | Sec A
- AKTU 1st Year Sem 1 Solved Paper 2015-16 | COMP. SYSTEM & C PROGRAMMING | Sec C
- AKTU 1st Year Sem 1 Solved Paper 2014-15 | COMP. SYSTEM & C PROGRAMMING | Sec A
- AKTU 1st Year Sem 2 Solved Paper 2014-15 | COMP. SYSTEM & C PROGRAMMING | Sec A
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



