Invitation

js

Wednesday, November 29, 2023

M3-R4: PROGRAMMING AND PROBLEM SOLVING THROUGH ‘C’ LANGUAGE [July, 2010, New]

 



M3-R4: PROGRAMMING AND PROBLEM SOLVING THROUGH ‘C’ LANGUAGE


NOTE:

TOTAL TIME: 3 HOURS TOTAL MARKS: 100

(PART ONE 40; PART TWO 60)


PART ONE

(Answer all the questions)


  1. Each question below gives a multiple choice of answers. Choose the most appropriate one and enter in the “tear-off” answer sheet attached to the question paper, following instructions therein. (1x10)


    1. By default a real number is treated as a

  1. float

  2. double

  3. long double

  4. integer


    1. Which of the following expression is equivalent to ++*ptr?

A) (*ptr)++

B) ++*(ptr)

  1. (ptr)*++

  2. (ptr)++*


    1. The default storage class of a ‘C’ variable is

  1. auto

  2. static

  3. extern

  4. register


    1. Which header file should be included to use functions like malloc() and calloc()?

  1. memory.h

  2. stdlib.h

  3. string.h

  4. dos.h

 



M3-R4: PROGRAMMING AND PROBLEM SOLVING THROUGH ‘C’ LANGUAGE


NOTE:

TOTAL TIME: 3 HOURS TOTAL MARKS: 100

(PART ONE 40; PART TWO 60)


PART ONE

(Answer all the questions)


  1. Each question below gives a multiple choice of answers. Choose the most appropriate one and enter in the “tear-off” answer sheet attached to the question paper, following instructions therein. (1x10)


    1. By default a real number is treated as a

  1. float

  2. double

  3. long double

  4. integer


    1. Which of the following expression is equivalent to ++*ptr?

A) (*ptr)++

B) ++*(ptr)

  1. (ptr)*++

  2. (ptr)++*


    1. The default storage class of a ‘C’ variable is

  1. auto

  2. static

  3. extern

  4. register


    1. Which header file should be included to use functions like malloc() and calloc()?

  1. memory.h

  2. stdlib.h

  3. string.h

  4. dos.h




    1. We can combine the following two statements into one using

char *p;

p = (char*) malloc(100);

  1. char p = *malloc(100);

  2. char *p = (char) malloc(100);

  3. char *p = (char*)malloc(100);

  4. char *p = (char *)(malloc*)(100);


    1. How many times "DOEACC" will get printed?

#include<stdio.h> int main()

{

int x;

for(x=-1; x<=10; x++)

{

if(x < 5)

continue; else

break; printf("DOEACC");

}

return 0;

}

  1. Infinite times

  2. 11 times

  3. 0 times

  4. 10 times


    1. Which of the following statement is correct about the following program?

#include<stdio.h> long fun(int num)

{

int i; long f=1;

for(i=1; i<=num; i++) f = f * i;

return f;

}

  1. The function calculates the value of 1 raised to power num

  2. The function calculates the square root of an integer

  3. The function calculates the factorial value of an integer

  4. None of the above


    1. In C, if you pass an array as an argument to a function, what actually gets passed?

  1. Value of elements in array

  2. First element of the array

  3. Base address of the array

  4. Address of the last element of array




    1. If a file is open in ‘write’ mode, then

  1. If it does not exist, an error is returned

  2. If it does not exist, it is created

  3. If it exists, then data is written at the end

  4. If it exists, error is returned


    1. Which of the following functions is used to free the allocated memory?

  1. remove(var-name);

  2. free(var-name);

  3. delete(var-name);

  4. dalloc(var-name);



  1. Each statement below is either TRUE or FALSE. Choose the most appropriate one and ENTER in the “tear-off” sheet attached to the question paper, following instructions therein. (1x10)


    1. Size of short integer and long integer can be verified using the sizeof() operator.

    2. The expression a[0] and *a[0] are same for int a[100].

    3. A structure can contain similar or dissimilar elements.

    4. Right shift of an unsigned integer by one bit is equivalent to multiplying it by two.

    5. Functions can be called either by value or by reference.

    6. Bounds of the array index are checked during execution.

    7. Every time we supply new set of values to the program at command prompt, we need to recompile the program.

    8. If the two strings are found to be unequal then strcmp returns difference between the first non- matching pair of characters.

    9. Singly-linked lists contain nodes which have a data field as well as a next field, which points to the next node in the linked list.

    10. In computer programming, the translation of source code into object code is done by a compiler.




  1. Match words and phrases in column X with the closest related meaning/ word(s)/phrase(s) in column Y. Enter your selection in the “tear-off” answer sheet attached to the question paper, following instructions therein. (1x10)


X

Y

3.1

group of related data of same type that share a common name

A.

menu selection

3.2

operator used to get value at address stored in a pointer

B.

array

3.3

linked list is a

C.

declaration

3.4

switch statement is often used for

D.

break

3.5

extern int i; is a

E.

*

3.6

gives the current position of the pointer

F.

integer value

3.7

statement ends the loop

G.

strcat()

3.8

recursion is a process

H.

dynamic data structure

3.9

by default, functions return

I.

strcmp()

3.10

to concatenate two strings we use

J.

where a function call itself



K.

ftell()


  1. Each statement below has a blank space to fit one of the word(s) or phrase(s) in the list below. Enter your choice in the “tear-off” answer sheet attached to the question paper, following instructions therein. (1x10)


A.

void

B.

calling function

C.

stdio.h

D.

goto

E.

4/8

F.

flowchart

G.

8/16

H.

exit

I.

return

J.

logical operators

K.

garbage

L.

->


    1. breaks the normal sequential execution of the program.

    2. Ovals are used to represent starting and ending points in the .

    3. _ are used when we want to test more than one condition and make decision.

    4. A pointer variable contains until it is initialized.

    5. When a function returns a structure, it must be assigned to a structure of identical type in the

_.

    1. A float is _ bytes wide, whereas a double is _ bytes wide.

    1. The operator can be used to access structures elements using a pointer to a structure variable.

    2. The keyword used to transfer control from a called function back to the calling function is

.

    1. If a function return type is declared as it cannot return any value.

    2. Input/output function prototypes and macros are defined in _.




PART TWO

(Answer any FOUR questions)


5.

  1. What is dynamic memory allocation? Mention four functions used for dynamic memory manipulation.

  2. What are command line arguments? Explain with the help of a suitable example.

  3. Define a two dimensional array ‘int a[10][10]’. Write a ‘C’ program to initialize this array with numbers between 0 and 99. Then print the contents of ‘a’.

(5+5+5)


6.

  1. Define Auto and Register variables in context of C. What is the basic difference between these variables?

  2. Write a program that takes as input an integer between 1-12 (both inclusive) and prints the month corresponding to that integer.

  3. Draw two flow charts to distinguish between break and continue statements.

(5+5+5)


7.

  1. Write a function to swap two integers. The function does not return any value.

  2. Write a ‘C’ program to create a file of integers. The file name is given by the user. The second input to the program is the number ‘n’ of positive integers to be written into the file starting from 1.

  3. What is union data type? Define a union ‘u’ to hold a integer, float and character variable.

(6+6+3)


8.

  1. Write a C program to construct a linear linked list in C to store student records. The record contains roll no. and total marks. The program stops when a negative roll no. is entered.

  2. Write a program to find out whether a given number is Prime or not.

(6+9)


9.

  1. What will be the output of the program? Explain step by step.

#include<stdio.h> void fun(int*, int*); int main()

{

int i=5, j=2; fun(&i, &j);

printf("%d, %d", i, j); return 0;

}

void fun(int *i, int *j)

{

*i = *i**i;

*j = *j**j;

}




  1. What will be the output of the program? Explain step by step.

#include<stdio.h> int main()

{

int arr[5], i=-1, z; while(i<5)

arr[i]=++i;


for(i=0; i<6; i++) printf("%d, ", arr[i]);

return 0;

}

  1. Point out the error in the following program. How you will modify the program to overcome from the error?

#include<stdio.h> struct emp

{

char name[20]; int age;

};

int main()

{

emp int xx; int a;

printf("%d\n", &a); return 0;

}

(5+5+5)

 


What are command line arguments? Explain with the help of a suitable example.

 

DOEACC

[M3-R4: PROGRAMMING AND PROBLEM SOLVING THROUGH ‘C’ LANGUAGE]

[ July, 2010 (New)]

 

Q.What are command line arguments? Explain with the help of a suitable example.

Answer:

Command line arguments are values provided to a program at the time of its execution through the command line or terminal. When you run a program from the command line, you can pass additional information to the program by including command line arguments after the program name. These arguments are accessed by the program and can be used to influence its behavior or provide input data.

In C, the main function can take two arguments: argc (argument count) and argv (argument vector).

argc: It represents the number of command line arguments passed to the program, including the program name itself.

argv: It is an array of strings (character pointers) that contains the actual command line arguments. The first element argv[0] is the name of the program.

Here's a simple example in C to illustrate command line arguments:

#include <stdio.h>

int main(int argc, char *argv[]) {
    // Check if there are at least two arguments (including the program name)
    if (argc < 2) {
        printf("Usage: %s <name>\n", argv[0]);
        return 1; // Exit the program with an error code
    }

    // Print a greeting using the provided name
    printf("Hello, %s!\n", argv[1]);

    return 0; // Exit the program successfully
}

 

In this example, the program expects at least one command line argument (in addition to the program name). If the user doesn't provide the expected argument, the program prints a usage message. If an argument is provided, the program uses it to greet the user.

Here's how you might run this program from the command line: 

./greet John

In this example, "greet" is the name of the compiled program, and "John" is the command line argument. The program would then output:

Hello, John!


What is dynamic memory allocation? Mention four functions used for dynamic memory manipulation.

 

DOEACC

[M3-R4: PROGRAMMING AND PROBLEM SOLVING THROUGH ‘C’ LANGUAGE]

[ July, 2010 (New)]

 

Q. What is dynamic memory allocation? Mention four functions used for dynamic memory manipulation. 

Answer: Dynamic memory allocation in programming refers to the process of allocating memory at runtime (during program execution) rather than at compile time. This allows programs to allocate memory as needed and release it when it's no longer required. Dynamic memory allocation is particularly useful when the size of data structures is not known at compile time or when memory needs to be managed more flexibly.

Four functions commonly used for dynamic memory manipulation in C are:

malloc() (Memory Allocation):

Prototype: void* malloc(size_t size);
Description: Allocates a specified number of bytes of memory. It returns a pointer to the beginning of the allocated memory block.
calloc() (Contiguous Allocation):

Prototype: void* calloc(size_t num, size_t size);
Description: Allocates a block of memory for an array of elements, each of a specified size. It initializes all bytes in the allocated memory to zero.
realloc() (Reallocate Memory):

Prototype: void* realloc(void* ptr, size_t size);
Description: Changes the size of the previously allocated memory block. It can be used to resize a previously allocated memory block, either increasing or decreasing its size.
free() (Free Memory):

Prototype: void free(void* ptr);
Description: Releases the memory block pointed to by the given pointer. It deallocates the memory previously allocated by malloc, calloc, or realloc.
Here's a brief explanation of each function:

malloc: Allocates a specified amount of memory, but the initial content is undefined.

calloc: Allocates a specified number of blocks of memory, each with a specified size. It initializes the memory to zero.

realloc: Changes the size of the allocated memory block. It can be used to resize or reallocate memory.

free: Releases the allocated memory, making it available for other uses.

It's important to use these functions carefully to avoid memory leaks or accessing memory that has been freed. Additionally, in C++, the new and delete operators are often used for dynamic memory allocation and deallocation.

Write a C program to construct a linear linked list in C to store student records. The record contains roll no. and total marks. The program stops when a negative roll no. is entered.

DOEACC

[M3-R4: PROGRAMMING AND PROBLEM SOLVING THROUGH ‘C’ LANGUAGE]

[ July, 2010 (New)]

 

Q. Write a C program to construct a linear linked list in C to store student records. The record contains roll no. and total marks. The program stops when a negative roll no. is entered. 

 

#include <stdio.h>
#include <stdlib.h>

// Structure to represent a student record
struct Student {
    int rollNo;
    float totalMarks;
    struct Student* next;
};

// Function to insert a new student record at the end of the linked list
struct Student* insertRecord(struct Student* head, int rollNo, float totalMarks) {
    struct Student* newStudent = (struct Student*)malloc(sizeof(struct Student));
    newStudent->rollNo = rollNo;
    newStudent->totalMarks = totalMarks;
    newStudent->next = NULL;

    if (head == NULL) {
        // If the list is empty, the new student becomes the head
        head = newStudent;
    } else {
        // Traverse the list to find the last node and append the new student
        struct Student* temp = head;
        while (temp->next != NULL) {
            temp = temp->next;
        }
        temp->next = newStudent;
    }

    return head;
}

// Function to display the student records
void displayRecords(struct Student* head) {
    printf("Student Records:\n");
    printf("Roll No\tTotal Marks\n");
    struct Student* temp = head;
    while (temp != NULL) {
        printf("%d\t%.2f\n", temp->rollNo, temp->totalMarks);
        temp = temp->next;
    }
}

// Function to free the memory allocated for the linked list
void freeList(struct Student* head) {
    struct Student* temp;
    while (head != NULL) {
        temp = head;
        head = head->next;
        free(temp);
    }
}

int main() {
    struct Student* head = NULL;
    int rollNo;
    float totalMarks;

    // Input student records until a negative roll number is entered
    while (1) {
        printf("Enter roll number (negative to stop): ");
        scanf("%d", &rollNo);

        // Check if the entered roll number is negative
        if (rollNo < 0) {
            break;
        }

        printf("Enter total marks: ");
        scanf("%f", &totalMarks);

        // Insert the student record into the linked list
        head = insertRecord(head, rollNo, totalMarks);
    }

    // Display the student records
    displayRecords(head);

    // Free the memory allocated for the linked list
    freeList(head);

    return 0;
}

contacts

1 Swasthyam Multispeciality Clinic 1st Floor, Lobby C, Supreme Tower, GS Rd, near Silkalay Store, Christian Basti, Guwahati, Assam 781005 92...