0% found this document useful (0 votes)
4 views34 pages

PDS Combined (1 - 30)

The document details a quiz on Programming in C and Data Structures taken on February 17, 2024, where the participant completed the quiz with a score of 100% in the first attempt and 90% in the second attempt. It includes questions on pointer declarations, memory addresses, and program outputs, with correct answers provided for each question. The quiz demonstrates the participant's understanding of pointers and their operations in C programming.

Uploaded by

poojayuva02200
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views34 pages

PDS Combined (1 - 30)

The document details a quiz on Programming in C and Data Structures taken on February 17, 2024, where the participant completed the quiz with a score of 100% in the first attempt and 90% in the second attempt. It includes questions on pointer declarations, memory addresses, and program outputs, with correct answers provided for each question. The quiz demonstrates the participant's understanding of pointers and their operations in C programming.

Uploaded by

poojayuva02200
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

SIET LMS

Programming in C and Data Structures

Started on Saturday, 17 February 2024, 10:29 AM Quiz navigation


State Finished 1 2 3 4 5 6 7 8 9
Completed on Saturday, 17 February 2024, 10:33 AM
Time taken 4 mins 3 secs 10 11 12 13 14 15 16
Marks 25.00/25.00
Grade 100.00 out of 100.00 Show one page at a time

Question 1 Finish review


Declare an integer pointer with variable name as a
Correct

Mark 1.00 out of


Answer: int *a; 
1.00

Flag question

Question 2 Declare a character pointer c


Correct

Mark 1.00 out of


Answer: char *c; 
1.00

Flag question

Question 3 Make the below integer pointer point to the integer variable a
Correct
int a;
Mark 1.00 out of
int *ptr;
1.00

Flag question

Answer: ptr=&a; 

Question 4 Print the value in the variable a using the pointer


Correct
int a=7;
Mark 1.00 out of
int *ptr;
1.00
ptr = &a;
Flag question

Answer: printf("%d",*ptr); 

Question 5 Is this intialization correct?


Correct
int a;
Mark 1.00 out of
int *ptr=&a;
1.00
If correct type Yes, else type No
Flag question

Answer: Yes 

Yes. This is a correct way to declare and initialize in same line

Question 6 Is this declaration correct? If it is correct say YES else say NO


Correct
int *ptr;
Mark 1.00 out of
int a;
1.00
*ptr = &a;
Flag question

Answer: NO 

No. It is not correct *ptr means the value in ptr. You have to set ptr to &a and not the value in ptr

Question 7 Write the statement to set the value of a to 7 using ptr. Not directly using a.
Correct
int a;
Mark 1.00 out of
int *ptr;
1.00
ptr = &a;
Flag question

Answer: *ptr=7; 

Question 8 What is the output of this program. If you think answer is an error type "ERROR"
Correct
#include <stdio.h>
Mark 1.00 out of
int main()
1.00
{
Flag question
int a,*ptr;
ptr = &a;
a = 3;
*ptr = 7;
printf("%d",a);

Answer: 7 

Question 9
What is the output of this program. If error type error
Correct
#include <stdio.h>
Mark 1.00 out of
1.00 int main()
{
Flag question
int a,*ptr;
ptr = &a;
a = 3;
*ptr = 7;
printf("%d",*&*ptr);

Answer: 7 

* and & cancel each other out. So *ptr is what gets printed

Question 10 * in ptr is alled as _______ operator


Correct

Mark 1.00 out of a. address


1.00
b. pointer
Flag question
c. de-reference 

d. reverse

e. porting

Your answer is correct.

Question 11 Write a program to declare a pointer ptr to point to the variable a


Correct
assign a value to given input using scanf
Mark 10.00 out
of 10.00 increment a using the pointer

Flag question

For example:

Input Result

7 8

Answer: (penalty regime: 0 %)

Reset answer

1 #include <stdio.h>
2 int main()
3 ▼ {
4 int a;
5 int *ptr = &a;
6 scanf("%d", &a);
7 (*ptr)++;
8 printf("%d",a);
9 return 0;
10 }

Input Expected Got

 7 8 8 

 8 9 9 

Passed all tests! 

Correct
Marks for this submission: 10.00/10.00.

Question 12
& is called the address  operator
Correct

Mark 1.00 out of


1.00

Flag question

Question 13 Declare a pointer to this structure named ptr


Correct
struct tag {
Mark 1.00 out of
int a;
1.00
char b;
Flag question
};

Answer: struct tag *ptr; 

Question 14 What is the output for this program if input is given as 3? If error, type ERROR
Correct

Mark 1.00 out of


1.00 #include <stdio.h>
int main()
Flag question
{
int a,*ptr;
ptr = &a;
scanf("%d",ptr);
printf("%d",a);
}

Answer: 3 

Question 15 What is the output of the program if input is given as 3? If error type ERROR
Correct
#include <stdio.h>
Mark 1.00 out of
int main()
1.00
{
Flag question
int a,*ptr;
ptr = &a;
scanf("%d",*ptr);
printf("%d",a);
}

Answer: ERROR 

*ptr cannot be the address of a. It has to be ptr or &a in scanf

Question 16
What is the output of this program. If error type error
Correct
#include <stdio.h>
Mark 1.00 out of
1.00 int main()

Flag question
{
int a,*ptr;
ptr = &a;
a = 3;
*ptr = 7;
printf("%d",*&*ptr);
}

Answer: 7 

Finish review
SIET LMS

Programming in C and Data Structures

Started on Saturday, 17 February 2024, 10:54 AM Quiz navigation


State Finished 1 2 3 4 5 6 7 8 9
Completed on Saturday, 17 February 2024, 10:59 AM
Time taken 5 mins 19 secs 10
Marks 9.00/10.00
Grade 90.00 out of 100.00 Show one page at a time

Question 1 Finish review


#include <stdio.h>
Correct int main()
Mark 1.00 out of {
1.00 int i;
Flag question int *ptr;
//write the statement which will make the pointer, point to variable i.
return 0;
}

Answer: ptr=&i; 

Question 2 Size of character and size of integer is the same. TRUE or FALSE
Correct

Mark 1.00 out of Select one:


1.00
True
Flag question
False 

YES. They are different. Character is 1 byte and integer is 2 or 4 bytes depending on the computer

Question 3 character and integer pointer are same size.


Correct

Mark 1.00 out of Select one:


1.00
True 
Flag question
False

Yes. Both pointers store the address. Hence they are same size.

Question 4
1. #include <stdio.h>
Correct
2. int main()
Mark 1.00 out of
3. {
1.00
4. int i;
Flag question
5. int *ptr;
6. ptr = &i;
7. i = 10;
8. printf("%d",ptr);
9. return 0;
10. }

Will the above program print 10 as the answer? YES or NO

Answer: NO 

Question 5
1. #include <stdio.h>
Correct
2. int main()
Mark 1.00 out of 3. {
1.00
4. int i;
Flag question
5. int *ptr;
6. ptr = &i;
7. i = 1.4;
8. printf("%d",*ptr);
9. return 0;
10. }

Answer: 1 

Question 6
1. #include <stdio.h>
Correct
2. int main()
Mark 1.00 out of 3. {
1.00
4. int i;
Flag question 5. int *ptr;
6. int *ptr1;
7. ptr = &i;
8. ptr1 = ptr;
9. *ptr1 = 2.2;
10. printf("%d",*ptr);
11. return 0;
12. }
13. What is the output of the program?

Answer: 2 

Question 7
1. #include <stdio.h>
Correct
2. int main()
Mark 1.00 out of 3. {
1.00
4. int i;
Flag question
5. int *ptr;
6. int *ptr1;
7. ptr = &i;
8.
9. ptr1 = ptr;
10.
11. *ptr1 = 3.3;
12. (*ptr1)++;
13. printf("%d",*ptr);
14. return 0;
15. }

Answer: 4 

Question 8
1. #include <stdio.h>
Incorrect
2. int main()
Mark 0.00 out of 3. {
1.00
4. int i;
Flag question
5. int *ptr;
6. int *ptr1;
7. ptr = &i;
8.
9. ptr1 = ptr;
10.
11. *ptr1 = 7.8;
12. *ptr1++;
13. printf("%d",*ptr);
14. return 0;
15. }
16. Find the output

Answer: 7 

Question 9
1. #include <stdio.h>
Correct
2. int main()
Mark 1.00 out of
3. {
1.00
4. int i;
Flag question
5. int *ptr;
6. ptr = &i;
7. i = 7.5;
8. *ptr++;
9. printf("%d",i);
10. return 0;
11. }

Answer: 7 

Question 10 What is the output


Correct
1. #include <stdio.h>
Mark 1.00 out of 2. int main()
1.00
3. {
Flag question 4. int i;
5. char *ptr1;
6.
7. ptr1 = &i;
8. i = 10;
9.
10. printf("%d",*ptr1);
11. return 0;
12. }

a. 10 is displayed and no warnings

b. 10 is displayed but warning on incompatible pointer is also shown 

c. Nothing is displayed

d. Error incompatible pointers

Your answer is correct.

Finish review
SIET LMS

Programming in C and Data Structures

Started on Saturday, 17 February 2024, 12:16 PM Quiz navigation


State Finished 1 2 3 4 5 6 7 8 9
Completed on Saturday, 17 February 2024, 12:20 PM
Time taken 4 mins 29 secs 10 11 12 13 14 15 16 17 18
Marks 20.00/20.00
Grade 100.00 out of 100.00 19 20

Question 1 What is the output of this program? Show one page at a time
Correct
#include <stdio.h>
Finish review
Mark 1.00 out of
int main()
1.00
{
Flag question
int *iptr;
char *cptr;
if(sizeof(iptr) == sizeof(cptr))
printf("True");
else
printf("False");

Select one:
True 

False

Pointer holds an address. Whether it is integer pointer or character pointer or structure pointer, it has the same size since it just stores the
address

Question 2 What is the output of this program?


Correct
#include <stdio.h>
Mark 1.00 out of
int main()
1.00
{
Flag question
int *a;
int arr[] = {1,2,3,4,5};
a = arr; //same as a = &arr[0]
a++;
printf("%d",*a);
return 0;
}

Answer: 2 

Question 3
What is the output of this program? If error, type "Error"#include <stdio.h>
Correct
int main()
Mark 1.00 out of
1.00 {
int *a;
Flag question
int arr[] = {1,2,3,4,5};
a = (int *) arr; //same as a = &arr[0]
a+=2;
printf("%d",*a);
return 0;
}

Answer: 3 

Question 4 Assuming size of integert to be 4 bytes. What would be the address integer ptr points to after the code
Correct
Assume ptr initially points to 1000000
Mark 1.00 out of
1.00 ptr = ptr + 4;

Flag question

Answer: 1000016 

ptr = ptr + 4 translates to


ptr = ptr + 4 * sizeof(int)

ptr = ptr + 16
So answer is 1000016

Question 5
Assuming size of integert to be 4 bytes. What would be the address integer ptr points to after the code
Correct
Assume ptr initially points to 1000000
Mark 1.00 out of
1.00 ptr = ptr + 4;
Flag question

Answer: 1000016 

Question 6
What would be the address character ptr points to after the code. Size of char is 1.
Correct
Assume ptr initially points to 1000000
Mark 1.00 out of
1.00

Flag question char *ptr;


ptr = ptr + 28;

Answer: 1000028 

Question 7
Assuming size of int to be 4 bytes
Correct
struct tag
Mark 1.00 out of
1.00 {
int a;
Flag question
int b;
};

What would be the address struct tag ptr points to after the code
Assume ptr initially points to 1000000

ptr = ptr + 3;

Answer: 1000024 

Question 8 Is the base address of the array same as the name of the array?
Correct
That is int arr[10];
Mark 1.00 out of
1.00 &arr[0] same as arr

Flag question
Select one:
True 

False

The base address of the array is same as the name of the array

That's why we use


scanf("%s",arr);
instead of
scanf("%s",&arr[0]);

Question 9 What is the output of the below code?


Correct main()
Mark 1.00 out of {
1.00 int a[5] = {1,2,3,4,5};
Flag question int *ptr = &a[0]; //same as ptr = a;
*(ptr+3) = 7;
printf("%d %d %d %d %d",a[0],a[1],a[2],a[3],a[4]);
}

Answer: 12375 

Question 10 What is the output of the below code?


Correct main()
Mark 1.00 out of {
1.00 int a[5] = { 13,13*10+1,13*10+2,13*30+2,13*40+3};
Flag question int *ptr = &a[0]; //same as ptr = a;
*(ptr+2) = 7;
printf("%d",a[1]+a[2]);
}

Answer: 138 

Question 11 What is the output? If error, type "Error"


Correct
#include <stdio.h>
Mark 1.00 out of
int main()
1.00
{
Flag question
int a[5] = {1,2,3,4,5};
*(a+1) = 7;
printf("%d %d %d %d %d",a[0],a[1],a[2],a[3],a[4]);
}

Answer: 17345 

Both a[1] or *(a+1) are the same.

Note array is a pointer where the base address cannot be modified


So

a[i] = *(a+i)

Question 12
What is the output? If error, type "Error"
Correct
#include <stdio.h>
Mark 1.00 out of
1.00 int main()
{
Flag question
int a[5] = {1,2,3,4,5};
int *ptr=a;
ptr[3] = 7;
printf("%d %d %d %d %d",a[0],a[1],a[2],a[3],a[4]);
}

Answer: 12375 

Yes pointer can be accessed like an array


*(ptr+3) is same as ptr[3]

Question 13 Assuming ptr to an integer pointer, is &ptr[3] same as ptr+3


Correct

Mark 1.00 out of Select one:


1.00
True 
Flag question
False

&ptr[3] is same as ptr+3

Question 14 Find the output of this program. If error type answer as Error
Correct
#include <stdio.h>
Mark 1.00 out of
int main()
1.00

Flag question
{
int a[5] = {1,2,3,4,5};
int *ptr=a;
ptr++;
*ptr = 3;
printf("%d %d %d %d %d",a[0],a[1],a[2],a[3],a[4]);
}

Answer: 13345 

Question 15
Find the output of this program. If error type answer as Error
Correct
#include <stdio.h>
Mark 1.00 out of
1.00 int main()
Flag question
{
int a[5] = {1,2,3,4,5};
int *ptr=a;
a++;
a[3] = 4;
printf("%d %d %d %d %d",a[0],a[1],a[2],a[3],a[4]);
}

Answer: Error 

The base address of an array cannot be modified

Question 16
Find the output of this program. If error type answer as Error
Correct
#include <stdio.h>
Mark 1.00 out of
1.00 int main()
Flag question {
int a[5] = {1,2,3,4,5};
int *ptr=a;
ptr = ptr + 2;
ptr[1] = 5;
printf("%d %d %d %d %d",a[0],a[1],a[2],a[3],a[4]);
}

Answer: 12355 

int a[5] = {1,2,3,4,5};


int *ptr=a;
//ptr points to the base address
ptr = ptr + 2;
//ptr points to & a[2]

ptr[1] = 5;
*(ptr+1) which is address of ptr (&a[2 + 1] which &a[3])
a]3] is set to 5
printf("%d %d %d %d %d",a[0],a[1],a[2],a[3],a[4]);

Question 17 What is the output of this program?


Correct
#include <stdio.h>
Mark 1.00 out of
int main()
1.00
{
Flag question
int a[5] = {1,2,3,4,5};
int *ptr=a;
ptr = ptr + 2;
ptr[1] = 5;
ptr--;
ptr[2] = 3;
ptr+=2;
*ptr = 10;
ptr = ptr-4;
*ptr = 17;

printf("%d %d %d %d %d",a[0],a[1],a[2],a[3],a[4]);
}

Answer: 1 2 3 10 5 

Question 18 What is the output of this code?


Correct
#include <stdio.h>
Mark 1.00 out of
int main()
1.00
{
Flag question
int a[5] = {1,2,3,4,5};
int i;
char *ptr = (char *)a;
for(i=0;i<5;++i)
{
*ptr = i*10;
ptr += 4;
}
printf("%d %d %d %d %d",a[0],a[1],a[2],a[3],a[4]);
}

Answer: 0 10 20 30 40 

20 bytes would have been allocated for the 5 integer pointers as given below in memory

00010002000300040005

Now the character pointer would have pointed to value 1 from the left when ptr = a was done. this is the lowest order byte in integer
Now in the loop the value is changed to i*10 so 0 is assigned
when ptr = ptr+4 is done, it points to address where the value 2 is stored
now i is 1 so i*10, 10 is assigned instead of 2
same continues till the last element

Question 19 What is the output of the below code?


Correct
#include <stdio.h>
Mark 1.00 out of
int main()
1.00
{
Flag question
char a[10] = {1,2,3,4,5,6,7,8,9};
int i;
int *ptr = (int *)a;
ptr ++;
*ptr = (char)20;
printf("%d %d %d %d %d",a[0],a[1],a[2],a[3],a[4]);
}

Answer: 1 2 3 4 20 

Question 20 What is the output? If error, type Error


Correct
#include <stdio.h>
Mark 1.00 out of
int main()
1.00
{
Flag question
char a[10] = {1,2,3,4,5,6,7,8,9};
a++;
*a = 20;
printf("%d %d %d %d %d",a[0],a[1],a[2],a[3],a[4]);
}

Answer: Error 

Base address of the array cannot be modified.

Finish review
SIET LMS

Programming in C and Data Structures

Started on Saturday, 17 February 2024, 12:33 PM Quiz navigation


State Finished 1 2 3 4 5 6 7 8 9
Completed on Saturday, 17 February 2024, 12:37 PM
Time taken 4 mins 25 secs 10
Marks 19.00/19.00
Grade 100.00 out of 100.00 Show one page at a time

Question 1 Finish review


What should be the code in the brackets
Correct

Mark 1.00 out of #include <stdio.h>


1.00
int mod( int *k  )
Flag question
{
*k = 4;
}
int main()
{
int a;
mod(&a);
}

You should have an integer pointer as the function argument whose name is l

Question 2 What should be the code given to set the value to 4?


Correct #include <stdio.h>
Mark 1.00 out of int mod( int *l)
1.00 {
Flag question
*l=4; 
}

int main()
{
int a;
mod(&a);
}

Question 3 What is the output of the code below?


Correct
#include <stdio.h>
Mark 1.00 out of
1.00 int mod( int *l)
{
Flag question
*l=7;
}
int main()
{
int a;
mod(&a);
printf("%d",a);
}

Answer: 7 

Question 4
What is the output of the code below?
Correct
#include <stdio.h>
Mark 1.00 out of
1.00 int mod( int a)
Flag question {
a=7;
}
int main()
{
int a=38;
mod(a);
printf("%d",a);
}

Answer: 38 

Question 5
What is the output of the code below?
Correct
#include <stdio.h>
Mark 1.00 out of
1.00 int mod( int *a)
Flag question {
*a=23;
}
int main()
{
int a=3;
mod(&a);
printf("%d",a);
}

Answer: 23 

Question 6 What is the output of the below code?


Correct
#include <stdio.h>
Mark 1.00 out of
1.00 int mod( int *a)
{
Flag question
a=7;
}

int main()
{
int a=5;
mod(&a);
printf("%d",a);
}

Answer: Error 

Error. It should be

*a=7;
not

a=7;
a=7; would set the address to 7 instead of the value to 7.

Question 7 Write a program to swap two variables using call by reference.


Correct

Mark 10.00 out


of 10.00 For example:
Flag question
Input Result

10 12 10
12

Answer: (penalty regime: 0 %)

Reset answer

1 #include <stdio.h>
2
3 //Write swap function here. swap to swap the values in a and b
4 //No changes should be made to main function
5 ▼ void swap(int *y, int*z){
6 int temp = *z;
7 *z = *y;
8 *y = temp;
9 }
10
11 int main()
12 ▼ {
13 int a,b;
14 scanf("%d %d",&a,&b);
15 swap(&a,&b);
16 printf("%d %d",a,b);
17 }

Input Expected Got

 10 12 10 12 10 
12

Passed all tests! 

Correct
Marks for this submission: 10.00/10.00.

Question 8 Is the following code an example of call by value or call by reference


Correct
#include <stdio.h>
Mark 1.00 out of
void modify(int *x)
1.00
{
Flag question
*x = 7;
}
int main()
{
int a = 3;
modify(&a);
printf("%d",a);
}

a. call by value

b. call by reference 

Your answer is correct.

Question 9
Is the following code an example of call by value or call by reference
Correct
#include <stdio.h>
Mark 1.00 out of
1.00 void modify(int x)
{
Flag question
x = 7;
}
int main()
{
int a = 3;
modify(a);
printf("%d",a);
}

a. call by value 

b. call by reference

Your answer is correct.

Question 10
Is the following code an example of call by value or call by reference
Correct
#include <stdio.h>
Mark 1.00 out of
1.00 int modify(int a)
{
Flag question
a=8;
return a;
}
int main()
{
int a = 3;
a=modify(a);
printf("%d",a);
}

a. call by value 

b. call by reference

Your answer is correct.

Finish review
SIET LMS

Programming in C and Data Structures

Started on Saturday, 17 February 2024, 2:06 PM Quiz navigation


State Finished 1 2 3 4 5 6 7 8 9
Completed on Saturday, 17 February 2024, 2:08 PM
Time taken 2 mins 34 secs 10
Marks 9.00/10.00
Grade 90.00 out of 100.00 Show one page at a time

Question 1 Finish review


Write the c statement to allocate 10 integers using malloc to an integer pointer ptr
Incorrect

Mark 0.00 out of


Answer: (int *)malloc(10*sizeof(int)); 
1.00

Flag question

Question 2 When you do dynamic memory allocation using malloc/calloc in which memory segment memory gets allocated?
Correct

Mark 1.00 out of a. stack


1.00
b. heap 
Flag question
c. text

d. data

e. queue

Your answer is correct.

Question 3
After malloc, the function used to set the entire memory region to a particular value, usually 0, is memset() 
Correct

Mark 1.00 out of


1.00

Flag question

Question 4 Which of the following header files must necessarily be included to use dynamic memory allocation functions?
Correct

Mark 1.00 out of


1.00 a. stdlib.h or alloc.h 
Flag question
b. stdio.h

c. string.h

d. math.h

e. dynmem.h

Your answer is correct.

Question 5 What is the return type of malloc function?


Correct

Mark 1.00 out of


Answer: void 
1.00

Flag question

Question 6
Which function is used to delete or release the allocated memory space?
Correct

Mark 1.00 out of


1.00

Flag question
Answer: free() 

Question 7 calloc() allocates the memory and also initializes the allocates memory to zero, while memory allocated using malloc() has random data.
Correct

Mark 1.00 out of


1.00 Select one:
Flag question True 

False

Question 8 calloc() allocates the memory and also initializes the allocates memory to zero
Correct

Mark 1.00 out of Select one:


1.00
True 
Flag question
False

Question 9
Correct A pointer that points somewhere, but not to a valid object is called dangling  pointer
Mark 1.00 out of
1.00

Flag question

Question 10 The calloc() function allocates space for an array of n objects, each of whose size is defined by size. Space is initialized to all bits zero
Correct

Mark 1.00 out of Select one:


1.00
True 
Flag question
False

Finish review
SIET LMS

Programming in C and Data Structures

Started on Saturday, 17 February 2024, 2:21 PM Quiz navigation


State Finished 1 2 3 4 5 6 7 8 9
Completed on Saturday, 17 February 2024, 2:31 PM
Time taken 9 mins 28 secs 10 11
Marks 11.00/11.00
Grade 100.00 out of 100.00 Show one page at a time

Question 1 Finish review


What is the output?
Correct
#include <stdio.h>
Mark 1.00 out of
#define X 1
1.00
int main()
Flag question
{
printf("%d",X);
}

Answer: 1 

Question 2 #include <stdio.h>


Correct #define X=1
Mark 1.00 out of int main()
1.00 {
Flag question printf("%d",X);
}

Answer: Error 

Macros cannot be initalized with =

#define X 1
is correct

not
#define X=1

Question 3 What is the output?


Correct
#include <stdio.h>
Mark 1.00 out of
#define X 1
1.00
int main()
Flag question
{
printf("%d",++X);
}

Answer: Error 

Yes it is an error. X cannot be incremented since it would have got substituted to 1 during preprocessor phase of compilation. So the code
that is getting compiled will be looking like

#include <stdio.h>
int main()
{

printf("%d",++1);
}

Question 4 Find the output


Correct #include <stdio.h>
Mark 1.00 out of #define X 2
1.00 int main()
Flag question {
printf("%d",X*X);
}

Answer: 4 

Question 5 Find the output


Correct
#include <stdio.h>
Mark 1.00 out of
#define X 2+3
1.00
int main()
Flag question
{
printf("%d",X*X);
}

Answer: 11 

It expands to 2+3*2+3 which is 2+6+3 which is 11

Question 6 Predict the output


Correct
#include <stdio.h>
Mark 1.00 out of
#define X 4+3
1.00
int main()
Flag question
{
printf("%d",X*X);
}

Answer: 19 

Question 7 What is the output?


Correct
#include <stdio.h>
Mark 1.00 out of
#define X (4+3)
1.00
int main()
Flag question
{
printf("%d",X*X);
}

Answer: 49 

This expands to 7*7 since it is enclosed in brackers. So answer is 49.

Question 8 #include <stdio.h>


Correct
#define X (4+2)
Mark 1.00 out of
int main()
1.00
{
Flag question
printf("%d",X*X);
}

Answer: 36 

It expands to (4+2)*(4+2) = 6*6 = 36

Question 9 #include <stdio.h>


Correct
#define SUM(x,y) x+y
Mark 1.00 out of
1.00 int main()
Flag question {

printf("%d",SUM(3,4));
}

Answer: 7 

Question 10 #include <stdio.h>


Correct
#define MUL(x,y) x*y
Mark 1.00 out of
1.00 int main()

Flag question {

printf("%d",MUL(3+1,4+1));
}

Answer: 8 

It expands to 3+4*1+1 = 8

Question 11
#include <stdio.h>
Correct
#define MUL(x,y) x*y
Mark 1.00 out of
1.00 int main()
Flag question
{

printf("%d",MUL(4+1,5+1));
}

Answer: 10 

It expands to 4+1*5+1

Finish review
SIET LMS

Programming in C and Data Structures

Started on Saturday, 17 February 2024, 2:31 PM Quiz navigation


State Finished 1 2 3 4 5 6 7
Completed on Saturday, 17 February 2024, 2:43 PM
Time taken 12 mins 3 secs
Show one page at a time
Marks 7.00/7.00
Grade 100.00 out of 100.00 Finish review

Question 1 What is the output of this program?


Correct
#include <stdio.h>
Mark 1.00 out of
#define con(a,b) a##b
1.00
int main()
Flag question
{
int xy=10;
int cd=20;
printf("%d %d",con(x,y),con(c,d));
return 0;
}

Answer: 10 20 

Question 2 #include <stdio.h>


Correct #define con(a,b) a##b
Mark 1.00 out of int main()
1.00 {
Flag question
int xy=10;
int cd=20;
con("pri","ntf")("%d %d",con(x,y),con(c,d));
return 0;
}

Answer: Error 

concatenation of tokens works - not when given as strings

Question 3 #include <stdio.h>


Correct #define con(a,b) a##b
Mark 1.00 out of int main()
1.00 {
Flag question int xy=10;
int cd=20;
con(pri,ntf)("%d %d",con(x,y),con(c,d));
return 0;
}

Answer: 10 20 

Question 4 #include <stdio.h>


Correct #define t(a,b) a##b
Mark 1.00 out of int main()
1.00 {
Flag question
t(in,t) xy=10;
int cd=20;

t(pri,ntf)("%d %d",t(x,y),t(c,d));
return 0;

Answer: 10 20 

Question 5 #include <stdio.h>


Correct #define t(a,b) a##b
Mark 1.00 out of int t(ma,in)()
1.00 {
Flag question t(in,t) xy=1;
int cd=2;
t(pri,ntf)("%d %d",t(x,y),t(c,d));
return 0;
}

Answer: 12 

String concatenation works for within main or on main function itself. Answer is 1 2

Question 6 #include <stdio.h>


Correct #define mkstr(s) #s
Mark 1.00 out of int main(void)
1.00 {
Flag question printf(mkstr(itvac));
return 0;
}
What is the output?

Answer: itvac 

String concatenation works for within main or on main function itself. Answer is 1 2
# is called the stringy operator. It converts the given input to a string.

Question 7 What is the output of the below code?


Correct
#include <stdio.h>
Mark 1.00 out of
#define t(s) #s
1.00
int main(void)
Flag question
{
printf(t(itvac) " rocks");
return 0;
}

Answer: itvac rocks 

Single # is called the stringfy operator. It converts the test to string

Finish review
SIET LMS

Programming in C and Data Structures

Started on Saturday, 17 February 2024, 8:28 PM Quiz navigation


State Finished 1 2 3 4 5 6 7 8 9
Completed on Saturday, 17 February 2024, 8:45 PM
Time taken 16 mins 40 secs 10 11
Marks 10.00/11.00
Grade 90.91 out of 100.00 Show one page at a time

Question 1 Finish review


Keyword used to declare a structure is struct 
Correct

Mark 1.00 out of


1.00

Flag question

Question 2 ___ operator is used to access a field within a structure


Correct

Mark 1.00 out of


Answer: . 
1.00

Flag question

Question 3 structure is a user defined data type in C


Correct

Mark 1.00 out of Select one:


1.00
True 
Flag question
False

Yes. Structure is a user defined data type

Question 4 Assuming size of int is 4 bytes what is the size of this structure
Correct
#include <stdio.h>
Mark 1.00 out of
struct s
1.00
{
Flag question
int s;
int k;
};

int main()
{
printf("%ld",sizeof(struct s));
return 0;
}

Answer: 8 bytes 

Question 5 struct s{
Correct int s;
Mark 1.00 out of int k;
1.00 }k;
Flag question
Is it a right way to declare a variable k of type struct s

Select one:
True 

False

Yes. It is correct

Question 6
struct s{
Correct
int s;
Mark 1.00 out of
int k;
1.00
}k,l;
Flag question
Is it a right way to declare a variable k and l of type struct s

Select one:
True 

False

Yes. It is correct

Question 7
struct s{
Correct
int s;
Mark 1.00 out of
int k;
1.00
};
Flag question
struct s k;
Is it a right way to declare a variable k of type struct s

Select one:
True 

False

Question 8 Write the code store a value 5 in field k


Incorrect
struct s{
Mark 0.00 out of
int s;
1.00
int k;
Flag question
}w;

int main()
{
// What should be the code that should come here to initialize the field k in variable w to 1.
return 0;

Answer: w.k=5; 

Question 9 Find the output:


Correct
#include <stdio.h>
Mark 1.00 out of
struct s
1.00
{
Flag question
int s;
int k;
};
int main()
{
struct s t = {1,2};
printf("%d",t.k);
return 0;
}

Answer: 2 

Question 10
Find the output
Correct
#include <stdio.h>
Mark 1.00 out of
1.00 struct s
{
Flag question
int rollno;
char x;
};
int main()
{
struct s t = {1,'A'};
printf("%c",t.x);
return 0;
}

Answer: A 

Question 11 struct tag


Correct {
Mark 1.00 out of int a;
1.00 int b;
Flag question };
int main()
{
struct tag t;
struct tag t1;
t.a = 100;

t1.b=30; 
}
write a statement to initialize field b in t1 to 30.

Finish review
SIET LMS

Programming in C and Data Structures

Started on Saturday, 17 February 2024, 8:58 PM Quiz navigation


State Finished 1 2 3 4 5 6 7 8 9
Completed on Saturday, 17 February 2024, 9:00 PM
Time taken 2 mins 27 secs 10 11 12 13
Marks 13.00/13.00
Grade 100.00 out of 100.00 Show one page at a time

Question 1 Finish review


What is the output of this program?
Correct Assume size of int is 4 bytes
Mark 1.00 out of
#include <stdio.h>
1.00
struct tag
Flag question
{
int a;
int b;
};

int main()
{
printf("%ld",sizeof(struct tag));
return 0;
}

Answer: 8 

Question 2
What is the output of this program?
Correct
Assume size of int is 4 bytes
Mark 1.00 out of
1.00 #include <stdio.h>
struct tag
Flag question
{
int a;
int b;
int c;
};

int main()
{
printf("%ld",sizeof(struct tag));
return 0;
}

Answer: 12 

Question 3
What is the output of this program?
Correct
Assume size of int is 4 bytes and sizeof char is 1 byte
Mark 1.00 out of
1.00 #include <stdio.h>
struct tag
Flag question
{
int a;
int b;
char c;
};
int main()
{
printf("%ld",sizeof(struct tag));
return 0;
}

Answer: 12 

Answer is not 9. It is 12. This is because C adds padding in structures so that every field ends in a bird boundary.

Question 4
What is the output of this program?
Correct
Assume size of int is 4 bytes and sizeof char is 1 byte
Mark 1.00 out of
1.00 #include <stdio.h>
struct tag
Flag question
{
int a;
int b;
char c;
int d;
};

int main()
{
printf("%ld",sizeof(struct tag));
return 0;
}

Answer: 16 

Question 5
What is the output of this program?
Correct
Assume size of int is 4 bytes and sizeof char is 1 byte
Mark 1.00 out of
1.00 #include <stdio.h>
struct tag
Flag question
{
int a;
int b;
char c;
char d;
};

int main()
{
printf("%ld",sizeof(struct tag));
return 0;
}

Answer: 12 

It is 12. Since the 2 integers occupy 8 bytes. The third & 4th character occupy 9th and 10th bytes. After that 2 bytes of padding are added.

Question 6
What is the output of this program?
Correct
Assume size of int is 4 bytes and sizeof char is 1 byte
Mark 1.00 out of
1.00 #include <stdio.h>
Flag question
struct tag
{
int a;
int b;
char c;
char d;
char e;

};
int main()
{
printf("%ld",sizeof(struct tag));
return 0;
}

Answer: 12 

Still occupies only 12 bytes. The first 2 integers occupy 4 bytes each. The 9th byte is occupied by char c the 10th byte is occupied by char d
and 11th byte is occupied by char e and one byte padding is done.

Question 7
What is the output of this program?
Correct
Assume size of int is 4 bytes and sizeof char is 1 byte
Mark 1.00 out of
1.00 #include <stdio.h>

Flag question
struct tag
{
int a;
int b;
char c;
char d;
char e;
int f;

};
int main()
{
printf("%ld",sizeof(struct tag));
return 0;
}

Answer: 16 

2 integers occupy 4 bytes each making it 8 bytes. The 3 characters occupy 1 byte each making it total of 3 bytes. So 8+3 = 11
One byte padding is added after this making it 12.

Finally 4 byte integer is added for f which makes the total as 16.

Question 8
What is the output of this program?
Correct
Assume size of int is 4 bytes and sizeof char is 1 byte
Mark 1.00 out of
1.00 #include <stdio.h>
struct tag
Flag question
{
char a;
char b;
};
int main()
{
printf("%ld",sizeof(struct tag));
return 0;
}

Answer: 2 

When only characters are part of the structure, no padding is added.

Question 9
What is the output of this program?
Correct
Assume size of int is 4 bytes and sizeof char is 1 byte
Mark 1.00 out of
1.00 #include <stdio.h>
struct tag
Flag question
{
char a[2];
};
int main()
{
printf("%ld",sizeof(struct tag));
return 0;
}

Answer: 2 

When only characters are present no padding is added.

Question 10 What is the size of the structure


Correct
include <stdio.h>
Mark 1.00 out of
#pragma pack(1)
1.00
struct tag
Flag question
{
int a;
char b;
char c;
};

int main()
{
printf("%ld",sizeof(struct tag));
return 0;
}

Answer: 6 

#pragma pack (1) informs the compiler that it has to be a packed structure with no padding.

Question 11
What is the size of the structure
Correct
include <stdio.h>
Mark 1.00 out of
1.00 #pragma pack(1)
struct tag
Flag question
{
int a;
char b;
};

int main()
{
printf("%ld",sizeof(struct tag));
return 0;
}

Answer: 5 

Question 12
What is the size of the structure
Correct
include <stdio.h>
Mark 1.00 out of
1.00 #pragma pack(1)
Flag question
struct tag
{
int a;
char b;
char c;
int d;
char e;
};
int main()
{
printf("%ld",sizeof(struct tag));
return 0;
}

Answer: 11 

Question 13 #include <stdio.h>


Correct
#pragma pack(1)
Mark 1.00 out of
1.00 struct tag
{
Flag question
int a[2];
char b[2];
char c[3];
};

int main()
{
printf("%ld",sizeof(struct tag));
return 0;
}

Answer: 13 

#pragma is a compiler directive. That is it is used to give directions to the compiler


#pragma pack(1) would ensure that there is no padding.

Finish review
SIET LMS

Programming in C and Data Structures

Started on Saturday, 24 February 2024, 12:12 PM Quiz navigation


State Finished 1 2 3 4 5 6 7 8 9
Completed on Saturday, 24 February 2024, 12:19 PM
Time taken 7 mins 19 secs 10
Marks 18.00/19.00
Grade 94.74 out of 100.00 Show one page at a time

Question 1 Finish review


A structure has the following definition
Correct struct tag
Mark 1.00 out of {
1.00 int a;
Flag char b;
question };

Define a statement to declare an array of type struct tag with 10 elements with variable name t

Answer: struct tag t[10]; 

Question 2 A structure has the following definition


Correct
struct tag
Mark 1.00 out of
{
1.00
char a;
Flag
char b;
question
};

Define a statement to declare an array of type struct tag with 25 elements with variable name x

Answer: struct tag x[25]; 

Question 3 A structure has the following definition


Incorrect
struct tag
Mark 0.00 out of
{
1.00
int a;
Flag
char b;
question
};
struct tag t[25];

int main()
{

// Write a statement to store the value 7 in 4th index of the array - member element a to 20
}

Answer: t[3].a = 7; 

Question 4 What is the output of this program?


Correct
A structure has the following definition
Mark 1.00 out of
1.00 struct tag
{
Flag
question int a;
char b;
};
struct tag x[20];
int main()
{
printf("%ld",sizeof(x));
}

Answer: 160 

Question 5 What is the output of the program


Correct
#include <stdio.h>
Mark 1.00 out of
#pragma pack(1)
1.00
struct tag
Flag
{
question
int a[3];
char b;
};
struct tag x[10];
int main()
{
printf("%ld",sizeof(x));
}

Answer: 130 

Given that it is packed structure each structure is 13 bytes. So total is 130 bytes

Question 6
What is the output of the program
Correct
#include <stdio.h>
Mark 1.00 out of
1.00 #pragma pack(1)
struct tag
Flag
question {
int a;
char b;
int c;
};

struct tag x[10];


int main()
{
printf("%ld",sizeof(x));
}

Answer: 90 

Given that it is packed structure each structure is 9 bytes with no padding. So total is 90 bytes

Question 7
What is the output of the code?
Correct
#include <stdio.h>
Mark 1.00 out of
1.00 #pragma pack(1)
struct tag
Flag
question {
int a;
char b;
int c;
struct
{
int x;
int y;
};
};
struct tag x[10];
int main()
{
printf("%ld",sizeof(x));
}

Answer: 170 

A structure within a structure is called a nested structure.

Question 8 Write the code to set the dateofbirth hour to 12


Correct
#include <stdio.h>
Mark 1.00 out of
1.00 struct student
{
Flag
question int rollno;
char name[10];
struct
{
int hr;
int min;
int sec;
}dateofbirth;
};
struct student x;
int main()
{
//type the code to set the dateofbirth hour to 12
}

Answer: x.dateofbirth.hr=12; 

Question 9 Write the code to set the dateofbirth sec to 60


Correct
#include <stdio.h>
Mark 1.00 out of
struct student
1.00
{
Flag
int rollno;
question
char name[10];
struct
{
int hr;
int min;
int sec;
}dateofbirth;
};
struct student x;

int main()
{
//type the code to set the dateofbirth sec to 40
}

Answer: x.dateofbirth.sec=60; 

Question 10 The student structure template has been given below


Correct
Write a program to declare an array of 60 students.
Mark 10.00 out
Read the number of students and then read the regno and roll no of N students.
of 10.00
store it in the array of structures
Flag
Display the regno and rollno of each student after storing in the structure.
question

NOTE: Each question will be manually reviewed to ensure that you used an array of structures. So do not store it iin a
variable/array and display

For example:

Input Result

3 714017101
714017101 101
101 714017102
714017102 102
102 714017103
714017103 103
103

Answer: (penalty regime: 0 %)

Reset answer

#include <stdio.h>
struct student
{
int regno;
int rollno;
};
int main()
{
int n;
struct student students[60];
scanf("%d", &n);
for (int i = 0; i < n; ++i){
scanf("%d", &students[i].regno);
scanf("%d", &students[i].rollno);
}
for (int i = 0; i < n; ++i){
printf("%d\n",students[i].regno);
printf("%d\n",students[i].rollno);
}
}

Input Expected Got

 3 714017101 714017101 
714017101 101 101
101 714017102 714017102
714017102 102 102
102 714017103 714017103
714017103 103 103
103

Passed all tests! 

Correct
Marks for this submission: 10.00/10.00.

Save the state of the flags

Finish review
SIET LMS

Programming in C and Data Structures

Started on Saturday, 24 February 2024, 1:45 PM Quiz navigation


State Finished 1 2 3 4 5 6 7 8 9
Completed on Saturday, 24 February 2024, 2:11 PM
Time taken 25 mins 54 secs 10
Marks 19.00/19.00
Grade 100.00 out of 100.00 Show one page at a time

Question 1 Finish review


For structure variable t of type struct tag, how do you set the member variable i to 4?
Correct
#include <stdio.h>
Mark 1.00 out of
struct tag
1.00
{
Flag question
int i;
int j;
};
int main()
{
struct tag t;
// how do you set the value of member i for variable t to 4?
}

Answer: t.i=4; 

Question 2
How do you set the value of member i for variable t to 4 using the pointer ptr?
Correct
#include <stdio.h>
Mark 1.00 out of
1.00 struct tag
{
Flag question
int i;
int j;
};

int main()
{
struct tag t;
struct tag *ptr;
ptr = &t;
// how do you set the value of member i for variable t to 4 using the pointer ptr?
}

Answer: ptr->i=4; 

Question 3 What is the output of the below code


Correct
#include <stdio.h>
Mark 1.00 out of
struct tag
1.00
{
Flag question
int i;
int j;
};
int main()
{
struct tag t;
struct tag *ptr;
ptr = &t;
ptr->i = 4;
(*ptr).i = 6;
printf("%d",t.i);
}

Answer: 6 

Question 4 Find the output


Correct
#include <stdio.h>
Mark 1.00 out of
struct tag
1.00
{
Flag question
int i;
int j;
};

int main()
{
struct tag t1,t2;
struct tag *pt1;
struct tag *pt2;
t1.i = 4;
t1.j = 5;
pt1 = &t1;
pt2 = &t2;
*pt2 = *pt1;
printf("%d %d",t2.i,t2.j);
}

Answer: 45 

Question 5 #include <stdio.h>


Correct
struct tag
Mark 1.00 out of
{
1.00
int i;
Flag question
int j;
};
int main()
{
struct tag t1;
struct tag *pt1;
t1.i = 6;
pt1 = &t1;
pt1->j = 7;
printf("%d %d",t1.i,t1.j);
}

Answer: 67 

Question 6 Write a program to declare a pointer to the structure.


Correct
Read the values into the 2 integers and display the values
Mark 10.00 out
of 10.00 For example:

Flag question
Input Result

5 5
6 6

Answer: (penalty regime: 0 %)

Reset answer

1 #include <stdio.h>
2 ▼ struct tag {
3 int i;
4 int j;
5 };
6
7 int main()
8 ▼ {
9 int i,j;
10 struct tag t;
11 struct tag *pt1;
12 scanf("%d %d",&i,&j);
13 //Write code to make the pointer to point to t and then assign the values for i and j using the ptr
14 pt1 = &t;
15 pt1->i = i;
16 pt1->j = j;
17 printf("%d\n%d",pt1->i,pt1->j);
18 }

Input Expected Got

 5 5 5 
6 6 6

 7 7 7 
8 8 8

Passed all tests! 

Correct
Marks for this submission: 10.00/10.00.

Question 7 Find the output


Correct
#include <stdio.h>
Mark 1.00 out of struct tag
1.00
{
Flag question
int i;
int j;
};
struct tag t[3];
int main()
{
struct tag *ptr;
ptr=t;
(*ptr).i = 10;
(*ptr).j = 20;
ptr++;
ptr++;
(*ptr).i = 30;
(*ptr).j = 40;

printf("%d %d %d %d %d %d",t[0].i,t[0].j,t[1].i,t[1].j,t[2].i,t[2].j);
}

Answer: 10 20 0 0 30 40 

Question 8 Find the output. If Error type Error


Correct

Mark 1.00 out of


1.00 #include <stdio.h>

Flag question struct tag


{
int i;
int j;
};

int main()
{
struct tag t;
struct tag *ptr;
ptr=&t;
*ptr.i = 10;
*ptr.j = 20;
printf("%d %d",t.i,t.j);
}

Answer: Error 

Question 9 #include <stdio.h>


Correct struct tag
Mark 1.00 out of {
1.00 int i;
Flag question int j;
};

int main()
{
struct tag t;
struct tag *ptr;
ptr=&t;

(*ptr).i = 10;

(*ptr).j = 20;
printf("%d %d",t.i,t.j);

Answer: 10 20 

Question 10 #include <stdio.h>


Correct
struct tag
Mark 1.00 out of
{
1.00
int i;
Flag question
int j;
};
int main()
{
struct tag t;
struct tag *ptr;
ptr=&t;
t.i = 10;
t.j = 20;

ptr->i++;
ptr->j++;

printf("%d %d",t.i,t.j);
}

Answer: 11 21 

Finish review
SIET LMS

Programming in C and Data Structures

Started on Saturday, 24 February 2024, 4:30 PM Quiz navigation


State Finished 1 2 3 4 5 6 7 8 9
Completed on Saturday, 24 February 2024, 4:40 PM
Time taken 9 mins 47 secs 10
Marks 10.00/10.00
Grade 100.00 out of 100.00 Show one page at a time

Question 1 Finish review


What is the output of the below code?
Correct
#include <stdio.h>
Mark 1.00 out of
union tag
1.00
{
Flag
int i;
question
char c;
int j;
};

int main()
{
union tag x;
x.j = 0;
x.i = 10;
printf("%d",x.j);
return 0;
}

Answer: 10 

Question 2
What is the output of the below code?
Correct
#include <stdio.h>
Mark 1.00 out of
1.00 struct tag
{
Flag
question int i;
char c;
int j;
};
int main()
{
struct tag x;
x.j = 0;
x.i = 10;
printf("%d",x.j);
return 0;
}

Answer: 0 

Question 3
What is the output of the below code?
Correct
#include <stdio.h>
Mark 1.00 out of
1.00 union tag

Flag
{
question int i;
char c;
int j;
};

int main()
{
union tag x;
x.c = 'A';
x.i = 10;
printf("%d",x.c);
return 0;
}

Answer: 10 

Question 4 What is the output of the below code?


Correct
#include <stdio.h>
Mark 1.00 out of
1.00 union tag
{
Flag
question int i;
char c;
int j;
int k;
};

int main()
{
union tag x;
x.i = 10;
x.j = 20;
printf("%d",x.k);
return 0;
}

Answer: 20 

Question 5 What is the output of this program?


Correct
#include <stdio.h>
Mark 1.00 out of
union tag
1.00
{
Flag
int i[4];
question
};
int main()
{
union tag k;
k.i[0] = 10;
k.i[1] = 20;
k.i[2] = 30;
k.i[3] = 40;
k.i[0] = 10;
printf("%d",k.i[1]);
return 0;
}

Answer: 20 

In this case 16 bytes are allocated one for each integer given that it is an array. Each of i[0] to i[3] have separate 4 bytes.
So it will not overwrite each other.

Question 6 What is the output?


Correct
#include <stdio.h>
Mark 1.00 out of
union tag
1.00
{
Flag
int i;
question
char a[4];
};

int main()
{
union tag k;
k.i = 0x01020304;
printf("%d",k.a[0]);
printf("%d",k.a[1]);
printf("%d",k.a[2]);
printf("%d",k.a[3]);
return 0;

Answer: 4321 

Question 7
What is the output?
Correct
#include <stdio.h>
Mark 1.00 out of
1.00 union tag
{
Flag
question int i;
int j;
};

int main()
{
union tag k;
k.i = 7;
k.j = 8;

printf("%d",k.i);
return 0;

Answer: 8 

Question 8 Each member in the union is allocated a unique memory location. True/False?
Correct

Mark 1.00 out of


Answer: False 
1.00

Flag
question

No. All share the same memory location

Question 9 Each member in a structure is allocated a unique memory location. True or False?
Correct

Mark 1.00 out of Select one:


1.00
True 
Flag
question False

Question 10 What is the output of this program?


Correct
#include <stdio.h>
Mark 1.00 out of
union tag
1.00
{
Flag
int i;
question
int j;
};
int main()
{
union tag k;
k.i = 3;
k.j = 4;
printf("%d",k.i*k.j);
return 0;
}

Answer: 16 

No. All share the same memory location. So the value of 4 will be there in both i and j and hence answer is 16.

Save the state of the flags

Finish review
SIET LMS

Programming in C and Data Structures

Started on Saturday, 24 February 2024, 4:41 PM Quiz navigation


State Finished 1 2 3 4 5 6 7 8 9
Completed on Saturday, 24 February 2024, 8:56 PM
Time taken 4 hours 14 mins 10 11 12 13 14 15 16 17 18
Marks 18.00/19.00
Grade 94.74 out of 100.00 19

Question 1 Most appropriate sentence to describe unions is Show one page at a time
Correct

a. Union are like structures Finish review


Mark 1.00 out of
1.00
b. Union contain members of different data types which share the same storage area in memory 
Flag
question c. Union is addition of two structure

d. Union should have the same data type while structures can have different data types

Your answer is correct.

Question 2
A single structure can have different data types
Correct

Mark 1.00 out of 


a. True
1.00

Flag
question
b. False

Your answer is correct.

Question 3 Which of the following structure declaration will throw an error?


Incorrect

Mark 0.00 out of a. struct temp{}s;


1.00 main(){}
Flag
question

b. struct temp{};
struct temp s;
main(){}

c. struct temp s;

struct temp{};
main(){}

d. None of the mentioned

Your answer is incorrect.

Question 4
Find the output
Correct

Mark 1.00 out of


#include<stdio.h>
1.00
struct course
Flag {
question int courseno;
char coursename[25];
};
int main()
{
struct course c[] = { {102, "Java"},
{103, "PHP"},
{104, "DotNet"}
};
printf("%d ", c[1].courseno);
printf("%s", c[2].coursename);
return ;
}

Output is 103 DotNet 

Question 5
Find the Output
Correct

Mark 1.00 out of


#include<stdio.h>
1.00
int main()
Flag {
question struct emp
{
char n[20];
int age;
};
struct emp e1 = {"Dravid", 23};
struct emp e2 = e1;
if(e1 == e2)
printf("The structure are equal");
return ;
}

Answer: Error 

Question 6
Find the output
Correct union details
Mark 1.00 out of {
1.00 int data;
char value[4];
Flag
question }det;
int main()
{
det.data=0X01020304;
printf("%d %d %d %d",det.value[0],det.value[ 1],det.value[2],det.value[3]);
}
Output is ____
____
____
____

Answer: 4321 

Question 7
Find the output
Correct
#include<stdio.h>
Mark 1.00 out of
1.00 struct Point
Flag {
question int x, y, z;
};

int main()
{
struct Point p1 = {.y = 0, .z = 1, .x = 2};
printf("%d %d %d", p1.x, p1.y, p1.z);
return 0;
}

Answer: 201 

Question 8
Find the Output
Correct Assume the program is executed in 64-bit compiler
Mark 1.00 out of union test
1.00 {
int x;
Flag
question char arr[8];
int y;
};

int main()
{
printf("%d", sizeof(union test));
return 0;
}
Output is ____

Answer: 8 

Question 9
Find the output
Correct Assume the program is executed in 64-bit compiler
Mark 1.00 out of struct {
1.00 short s[5];
union {
Flag
question float y;
long z;
}u;
} t;

int main()
{
printf("%d", sizeof(t));
return 0;
}

Output is 24  bytes
Blank 1
Formula Editor

Question 10
Find the output
Correct struct tag
Mark 1.00 out of {
1.00 char a1:1;
char a2:1;
Flag
question char a3:1;
char a4:1;
char a5:1;
char a6:1;
char a7:1;
char a8:1;
}t;

int main()
{
printf("%d bytes",sizeof(t));
return 0;
}

Output is ___bytes

Answer: 1 

Question 11
Find the output
Correct

Mark 1.00 out of #include <stdio.h>


1.00
struct p
Flag {
question unsigned int x : 7;
unsigned int y : 2;
};
int main()
{
struct p p;
p.x = 110;
p.y = 2;
printf("%d\n", p.x);
}
Output is ____

Answer: 110 

Question 12
Find the output
Correct

Mark 1.00 out of #include <stdio.h>


1.00
struct p
Flag {
question unsigned int x : 7;
unsigned int y : 2;
};
int main()
{
struct p p;
p.x = 110;
p.y = 2;
printf("%d\n", p.x);
}
Output is ____

Answer: 110 

Question 13
What would be the size of the following union declaration? (Assuming size of double = 8, size of int = 4, size of char = 1)
Correct #include <stdio.h>
Mark 1.00 out of union uTemp
1.00 {
double a;
Flag
question int b[10];
char c;
}u;

Output is 40 

Question 14
Find the output
Correct #include <stdio.h>
Mark 1.00 out of struct p
1.00 {
unsigned int x : 2;
Flag
question unsigned int y : 2;
};
int main()
{
struct p p;
p.x = 3;
p.y = 4;
printf("%d\n", p.y);
}
Output is ___

Answer: 0 

Question 15
What will be the output of the following C code (Assuming size of int and float is 4)?
Correct #include <stdio.h>
Mark 1.00 out of union
1.00 {
int ival;
Flag
question float fval;
} u;
void main()
{
printf("%d", sizeof(u));
}
Output is ____

Answer: 4 

Question 16
What will be the output of the following C code?
Correct #include <stdio.h>
Mark 1.00 out of struct student
1.00 {
};
Flag
question void main()
{
struct student s[2];
printf("%d", sizeof(s));
}

Output is 0 

Question 17
What will be the output of the following C code?
Correct #include <stdio.h>
Mark 1.00 out of struct student
1.00 {
int no;
Flag
question char name[20];
};
void main()
{
struct student s;
s.no = 8;
printf("hello");
}

Output is hello 

Question 18
What will be the output of the following C code?
Correct #include <stdio.h>
Mark 1.00 out of void main()
1.00 {
struct student
Flag
question {
int no;
char name[20];
};
struct student s;
s.no = 8;
printf("%d", s.no);
}
Output is ____

Answer: 8 

Question 19
What would be the size of the following union declaration? (Assuming size of double = 8, size of int = 4, size of char = 1)
Correct #include <stdio.h>
Mark 1.00 out of union uTemp
1.00 {
double a;
Flag
question int b[10];
char c;
}u;

Output is 40 

Save the state of the flags

Finish review
SIET LMS

Programming in C and Data Structures

Started on Saturday, 24 February 2024, 9:17 PM Quiz navigation


State Finished 1 2 3
Completed on Saturday, 24 February 2024, 9:57 PM
Time taken 40 mins 40 secs
Show one page at a time
Marks 30.00/30.00
Grade 100.00 out of 100.00 Finish review

Question 1 Write a program to read the content of the file input.txt and print the output
Correct
The file contains a set of integers one in each line.
Mark 10.00 out
Read and the content and display the integer one line at a time for given N rows
of 10.00
For example:
Flag question

Input Result

3 10
20
30

4 10
20
30
100

Answer: (penalty regime: 0 %)

Language c

1 #include <stdio.h>
2 ▼ int main(){
3 FILE *fp;
4 int n;
5 fp = fopen("input.txt", "r");
6 scanf("%d",&n);
7
8 ▼ for(int i=0; i<n; i++){
9 int num;
10 if(fscanf(fp, "%d", &num) == 1)
11 printf("%d\n",num);
12
13 }
14 fclose(fp);
15 }

Input Expected Got

 3 10 10 
20 20
30 30

 4 10 10 
20 20
30 30
100 100

Passed all tests! 

Correct
Marks for this submission: 10.00/10.00.

Question 2 Write a program to read the content in a file input.txt and display the output
Correct The input in the file is a set of names one in each line
Mark 10.00 out
of 10.00 Answer: (penalty regime: 0 %)
Flag question
Language c

1 #include <stdio.h>
2
3 ▼ int main() {
4 FILE *filePointer;
5 char name[100];
6 filePointer = fopen("input.txt", "r");
7 ▼ while (fgets(name, sizeof(name), filePointer) != NULL) {
8 printf("%s", name);
9 }
10 fclose(filePointer);
11
12 return 0;
13 }
14
15

Passed all tests! 

Correct
Marks for this submission: 10.00/10.00.

Question 3 Write a program to read a given string from keyboard and store it in a file. Read the sentence from the file and display
Correct

Mark 10.00 out Answer: (penalty regime: 0 %)


of 10.00
Language c
Flag question
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 ▼ int main() {
5 FILE *filePointer;
6 char sentence[100];
7 fgets(sentence, sizeof(sentence), stdin);
8 filePointer = fopen("output.txt", "w");
9
10 fputs(sentence, filePointer);
11 fclose(filePointer);
12 filePointer = fopen("output.txt", "r");
13
14 ▼ while (fgets(sentence, sizeof(sentence), filePointer) != NULL) {
15 printf("%s", sentence);
16 }
17
18 fclose(filePointer);
19
20 return 0;
21 }
22

Input Expected Got

 helloworld helloworld helloworld 

Passed all tests! 

Correct
Marks for this submission: 10.00/10.00.

Finish review
SIET LMS

Programming in C and Data Structures

Started on Sunday, 25 February 2024, 1:00 PM Quiz navigation


State Finished 1 2 3 4 5 6 7 8 9
Completed on Sunday, 25 February 2024, 1:03 PM
Time taken 3 mins 8 secs 10 11 12 13 14 15
Marks 15.00/15.00
Grade 100.00 out of 100.00 Show one page at a time

Question 1 Finish review


Big O notation represents _______________ case analysis
Correct

Mark 1.00 out of a. average


1.00
b. best
Flag question
c. worst 

d. None of the above

Your answer is correct.

Question 2 Omega Ω represents __________ case analysis


Correct

Mark 1.00 out of


1.00
a. average
Flag question
b. Best 

c. None of the above

d. worst

Your answer is correct.

Question 3 The Theta notation repreents _________ case analysis of an algorithm


Correct

Mark 1.00 out of a. best


1.00
b. None
Flag question
c. worst

d. average 

Your answer is correct.

Question 4 4 Algorithms have the following Big-O complexities. Which one is the best (shortest execution time)?
Correct

Mark 1.00 out of a. O(N)


1.00
b. O(Log N)
Flag question
c. O(N Log N)

d. O(1) 

Your answer is correct.

Question 5
Algorithms have the following Big-O complexities. Which one is the best (shortest execution time)?
Correct

Mark 1.00 out of


1.00
a. O(Log N) 
Flag question
b. O(N log N)

c. O(N * N)

d. O(N)

Your answer is correct.

Question 6 What is the best case complexity for Linear Search? It is Ω(____)?
Correct

Mark 1.00 out of


Answer: 1 
1.00

Flag question

Question 7 What is the worst case complexity for linearsearch? It is O(__)


Correct

Mark 1.00 out of


Answer: n 
1.00

Flag question

Question 8
Searching for the first element in a linear search represents which compexity?
Correct

Mark 1.00 out of 


a. best case
1.00

Flag question b. worst case

c. None of the above

d. average case

Your answer is correct.

Question 9
Searching for the last element in a linear search represents which compexity?
Correct

Mark 1.00 out of


a. best case
1.00

Flag question b. None of the above

c. average case

d. worst case 

Your answer is correct.

Question 10 What is the worst case complexity of the below algorithm


Correct
for(I=1;I<=N;++I)
Mark 1.00 out of
1.00 a[i] = i

Flag question
a. O(N LOG N)

b. O(N) 

c. O(LOG N)

d. O(1)

Your answer is correct.

Question 11
What is the worst case complexity of the below algorithm
Correct
for(i=1;i<=N;++i)
Mark 1.00 out of
1.00 for(j=1;j<N;++j)
a[i] = j;
Flag question

a. O(N + N)

b. O(N * N) 

c. O(LOG N)

d. O(N)

Your answer is correct.

Question 12 Which among the following takes the longest execution time
Correct

Mark 1.00 out of a. O(N * N)


1.00
b. O(N * N * N)
Flag question
c. O(2 power N)

d. ON!) (N Factorial) 

Your answer is correct.

Question 13 What is the time worst case complexity of the following code
Correct main()
Mark 1.00 out of {
1.00 int i=1,n;
Flag question for(i=1;i<=n;++i)
printf("%d",i);
}

Select one:
A. O(C^(n-1)+ 1)
B. O(C^(n-1)-1)
C. O(N) 
D. O(C^n)

Question 14 What is the time complexity of the following code


Correct int i,j,k=0,n;
Mark 1.00 out of for(i=n/2;i<=n;i++)
1.00 {
Flag question for(j=2;j<=n;j=j*2)
{
k=k+n/2;
}
}

Select one:
A. O(N/2 log(N))
B. O(N*N)
C. O(N^2 log(N))
D. O(N log(N)) 

Question 15 What is the time complexity of the following code


Correct int k=1;
Mark 1.00 out of for(int i=1;i<=n;i++)
1.00 {
for(int j=1;j<=sqrt( n );j++)
Flag question {
k+=1;
}
}

Select one:
A. O(N*(2*sqrt(N))
B. O(N*(log(N))) 
C. O(N*N*log(N))
D. O(N*(2*log(N)))

Finish review
SIET LMS

Programming in C and Data Structures

Started on Sunday, 25 February 2024, 1:04 PM Quiz navigation


State Finished 1 2 3 4 5
Completed on Sunday, 25 February 2024, 1:07 PM
Time taken 2 mins 50 secs
Show one page at a time
Marks 5.00/5.00
Grade 100.00 out of 100.00 Finish review

Question 1 Which of the following data structure is linear type?


Correct

Mark 1.00 out of a. lists


1.00
b. queues
Flag question
c. strings

d. all of the above 

Question 2 Which of the following data structure non-linear type?


Correct

Mark 1.00 out of a. stack


1.00
b. lists
Flag question
c. trees 

d. strings

Question 3 The simplest type of data structure is __________?


Correct

Mark 1.00 out of a. multi dimensional array


1.00
b. linear array 
Flag question
c. two dimensional array

Question 4 Pointers store the next data element of a list


Correct

Mark 1.00 out of Select one:


1.00
True
Flag question
False 

Question 5 ADT is called as abstract because,_____________?


Correct

Mark 1.00 out of a. implementation details are hidden 


1.00
b. it is a collection of different data types
Flag question
c. it is complexity independent data type

Finish review
SIET LMS

Programming in C and Data Structures


Quiz navigation
Question 6 Develop a program to print the addition and subtraction of two 2D matrices.
Warning: these links will not save your answers. Use the next button at
Incomplete The dimensions of the matrices are given as input first. the bottom of the page.
answer
1 2 3 4 5 6 7
Example:
Marked out of
10.00
Input:
22 Finish attempt ...
Flag
question
22

12
34

45
67

Output:
Addition:
5
7
9
11
Subtraction:
-3
-3
-3
-3

For example:

Input Result

-2 -2 Row and column size should not be negative


-2 -2

3 4 Row and column size should be same for 2 matrices


1 2

Answer: (penalty regime: 0 %)

Language c

#include <stdio.h>

int main() {
int a, b, c, d;
scanf("%d %d", &a, &b);
scanf("%d %d", &c, &d);

if (a < 0 || b < 0 || c < 0 || d < 0) {


printf("Row and column size should not be negative");
return 0;
}
if (a != c || b != d) {
printf("Row and column size should be the same for two matrices");
return 0;
}

int matrix1[a][b], matrix2[c][d];


for (int i = 0; i < a; i++) {
for (int j = 0; j < b; j++) {
scanf("%d", &matrix1[i][j]);
}
printf("\n");
}

for (int i = 0; i < c; i++) {


for (int j = 0; j < d; j++) {
scanf("%d", &matrix2[i][j]);
}
printf("\n");
}
printf("Addition:\n");
for (int i = 0; i < a; i++) {
for (int j = 0; j < b; j++) {
printf("%d ", matrix1[i][j]+matrix2[i][j]);
}
printf("\n");
}
printf("Subtraction:\n");
for (int i = 0; i < a; i++) {
for (int j = 0; j < b; j++) {
printf("%d ",matrix1[i][j] - matrix2[i][j];);
}
printf("\n");
}
return 0;
}

An unexpected error occurred. The sandbox may be down. Try again shortly.

Check

Failed to run tests


Unexpected error while executing your code. The sandbox server may be down or overloaded. Perhaps try again shortly?

The submission was invalid, and has been disregarded without penalty.

Previous page Next page


SIET LMS

Programming in C and Data Structures


Quiz navigation
Question 7 Develop a program to print primary/principal and secondary/anti-diagonal diagonals of a matrix.
Incomplete Warning: these links will not save your answers. Use the next button at

answer
Example: Consider a matrix, { { 1, 5, 8 } , { 4, 3, 1 } , { 6, 5, 2 } }. Primary/Principal and secondary/Anti-diagonal elements are as follows, the bottom of the page.
1 2 3 4 5 6 7
Marked out of
10.00
Finish attempt ...
Flag
question

For example:

Input Result

2 Primary diagonal elements:


2 1
1 4
2 Secondary diagonal elements:
3 2
4 3

-2 Row and column size should not be negative


-2

Answer: (penalty regime: 0 %)

Language c

#include <stdio.h>
int main() {
int rows, cols;

scanf("%d", &rows);
scanf("%d", &cols);

if (rows < 0 || cols < 0) {


printf("Row and column size should not be negative");
return 1;
}
if (rows != cols) {
printf("Row and column size should be the same");
return 1;
}

int matrix[rows][cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d\n", &matrix[i][j]);
}
}

printf("Primary diagonal elements:\n");


for (int i = 0; i < rows; i++) {
printf("%d\n", matrix[i][i]);
}

printf("Secondary diagonal elements:\n");


for (int i = 0; i < rows; i++) {
printf("%d\n", matrix[i][rows - i - 1]);
}

return 0;
}

An unexpected error occurred. The sandbox may be down. Try again shortly.

Check

Failed to run tests


Unexpected error while executing your code. The sandbox server may be down or overloaded. Perhaps try again shortly?

The submission was invalid, and has been disregarded without penalty.

Previous page Finish attempt ...


SIET LMS

Programming in C and Data Structures

Time left 0:33:31


Quiz navigation

1 2 3 4 5 6 7 8 9
Question 10 Write a program to find if a given matrix is a sparse matrix or not? Assume matrix size to be 3x3
Correct
A sparse matrix is one in which majority elements are zero 10 11
Marked out of
10.00 For example:
Finish attempt ...
Flag question
Input Result

1 1 0 YES
0 0 2
0 0 0

Answer: (penalty regime: 0 %)


1 #include <stdio.h>
2 ▼ int main(){
3 int arr[50][50],spar=0;
4 ▼ for(int i=0; i<3; i++){
5 ▼ for(int j=0; j<3; j++){
6 scanf("%d",&arr[i][j]);
7 ▼ if(arr[i][j] == 0){
8 spar +=1;
9 }
10
11 }
12 }
13 double fl = spar/9.0;
14 ▼ if(fl > 0.5){
15 printf("YES");
16 }
17 ▼ else{
18 printf("NO ");
19 }
20 }

Check

Input Expected Got

 1 1 0 YES YES 
0 0 2
0 0 0

 1 1 1 NO NO 
0 0 2
0 0 2

Passed all tests! 

Previous page Next page


SIET LMS

Programming in C and Data Structures


Quiz navigation
Question 11 Write a program to build a Sparse Matrix representation of the below matrix
Correct Warning: these links will not save your answers. Use the next button at
the bottom of the page.
Marked out of 1 2 3 4 5 6 7 8 9
10.00

Flag 10 11
question

Finish attempt ...

For example:

Result

0 0 1 1 3 3
2 4 2 3 1 2
3 4 5 7 2 6

Answer: (penalty regime: 0 %)

Reset answer

#include<stdio.h>

int main(){
int arr[4][5] =
{
{0 , 0 , 3 , 0 , 4 },
{0 , 0 , 5 , 7 , 0 },
{0 , 0 , 0 , 0 , 0 },
{0 , 2 , 6 , 0 , 0 }
};
int arr1[10],arr2[10],arr3[10],c=0;
for(int i=0;i<4;i++){
for(int j=0;j<5;j++){
if(arr[i][j] != 0 ){
arr1[c] = i;
arr2[c] = j;
arr3[c] = arr[i][j];
c++;
}
}
}
for(int i=0;i<6;i++){
printf("%d ",arr1[i]);
}
printf("\n");
for(int i=0;i<6;i++){
printf("%d ",arr2[i]);
}
printf("\n");
for(int i=0;i<6;i++){
printf("%d ",arr3[i]);
}
}

Check

Expected Got

 0 0 1 1 3 3 0 0 1 1 3 3 
2 4 2 3 1 2 2 4 2 3 1 2
3 4 5 7 2 6 3 4 5 7 2 6

Passed all tests! 

Previous page Finish attempt ...


SIET LMS

Programming in C and Data Structures

Started on Thursday, 7 March 2024, 1:49 PM Quiz navigation


State Finished 1 2 3 4 5 6 7 8 9
Completed on Tuesday, 21 May 2024, 3:27 PM
Time taken 75 days 1 hour 10
Marks 25.00/28.00
Grade 89.29 out of 100.00 Show one page at a time

Question 1
Finish review
Construct a singly linked list from the memory map given below.
Correct For ease of construction address is between 1000 to 1120.
Mark 10.00 out
Assume head to point to 1012.
of 10.00
Answer should be typed like 33->44->523->63->NULL
Flag question
No spaces can be typed anywhere. Only the data in the node followed by the data in next node

Answer: 36->93->883->1120->1000->NULL 

Question 2 Construct the singly linked list with the given memory dump
Correct
Assume head starts at 1043
Mark 10.00 out
of 10.00 Answer should be in the format 123->345->432->443->NULL
only data elements in the link should be present
Flag question

Answer: 1120->958->999->273->123->780->1000->NULL 

Question 3 Construct the singly linked list for the given memory dump. Head is pointing to memory location 1033
Incorrect
Answer should be in the form 123->234->432->NULL
Mark 0.00 out of
Each value represents the data element
1.00

Flag question

Answer: 1892->1043->1000->1000->1076->1862->NULL 

Question 4
Assume head of a singly linked list is pointing to memory location 1033
Correct
Which memory location will be impacted if the following command is executed to the below memory location
Mark 1.00 out of
1.00 head.data = 33

Flag question
Asssume the reference occupies one byte memory location and data occupies one byte of memory location
NOTE: In actual, data and memory location would occupy at leaset 4 bytes of memory

Answer: 1033 

1033 location will have a value of 33 after executing the statement

Question 5
Assume head of a singly linked list is pointing to memory location 1033
Not answered

Marked out of
1.00

Flag question

If you display the singly linked list from head to last node after executing the following command what will be the output.
head.next.next.next=NULL;
Answer should be typed in the following format
43->72->63->71->NULL
Asssume the reference occupies one byte memory location and data occupies one byte of memory location
NOTE: In actual, data and memory location would occupy at leaset 4 bytes of memory

Answer: 

Question 6
Assume head of a singly linked list is pointing to memory location 1033
Correct

Mark 1.00 out of


1.00

Flag question

If you display the singly linked list from head to last node after executing the following command what will be the output.
head.next.next.data=1030;
Answer should be typed in the following format
43->72->63->71->NULL
Asssume the reference occupies one byte memory location and data occupies one byte of memory location
NOTE: In actual, data and memory location would occupy at leaset 4 bytes of memory

Answer: 1892->1043->1030->1000->1706->1862->NULL 

Question 7
Assume head of a singly linked list is pointing to memory location 1033
Correct

Mark 1.00 out of


1.00

Flag question

If you display the singly linked list from head to last node after executing the following command what will be the output.
head=head.next;
Answer should be typed in the following format
43->72->63->71->NULL

Asssume the reference occupies one byte memory location and data occupies one byte of memory location
NOTE: In actual, data and reference(pointer) would occupy at leaset 4 bytes of memory each

Answer: 1043->1000->1000->1706->1862->NULL 

Question 8
Assume head of a singly linked list is pointing to memory location 1033
Not answered

Marked out of
1.00

Flag question

If you display the singly linked list from head to last node after executing the following command what will be the output.
head.next = head.next.next;

Answer should be typed in the following format


43->72->63->71->NULL
Asssume the reference occupies one byte memory location and data occupies one byte of memory location
NOTE: In actual, data and reference(pointer) would occupy at leaset 4 bytes of memory each

Answer: 

We have essentially skipped the second node.

The memory dump which was initially this

This is how the memory would be after the head.next = head.next.next is executed

Question 9
Assume head of a singly linked list is pointing to memory location 1033
Correct

Mark 1.00 out of


1.00

Flag question

If you display the singly linked list from head to last node after executing the following command what will be the output.
head.next = NULL
Answer should be typed in the following format
43->72->63->71->NULL

Asssume the reference occupies one byte memory location and data occupies one byte of memory location
NOTE: In actual, data and reference(pointer) would occupy at leaset 4 bytes of memory each

Answer: 1892->NULL 

Question 10
Assume head of a singly linked list is pointing to memory location 1033
Correct

Mark 1.00 out of


1.00

Flag question

If you display the singly linked list from head to last node after executing the following instructions what will be the output.
head.next.data = 33;
head.next.next = NULL;

Answer should be typed in the following format


43->72->63->71->NULL

Asssume the reference occupies one byte memory location and data occupies one byte of memory location
NOTE: In actual, data and reference(pointer) would occupy at leaset 4 bytes of memory each

Answer: 1892->33->NULL 

Finish review
SIET LMS

Programming in C and Data Structures

Started on Monday, 10 June 2024, 4:00 PM Quiz navigation


State Finished 1
Completed on Monday, 10 June 2024, 4:11 PM
Time taken 10 mins 19 secs
Finish review

Question 1 Write a program to add a node to singly linked list.


Correct
For example:
Marked out of
10.00 Input Result
Flag question
5 10 20 30 40 50
10
20
30
40
50

Answer: (penalty regime: 0 %)

Language c

1 #include<stdio.h>
2 #include<stdlib.h>
3 ▼ struct node{
4 int data;
5 struct node *link;
6 };
7 struct node *head=NULL;
8 struct node *tail=NULL;
9 ▼ void add(int val){
10 struct node *temp=(struct node*)malloc(sizeof(struct node));
11 temp->data=val;
12 temp->link=NULL;
13 ▼ if(head==NULL){
14 head=temp;
15 tail=temp;
16 ▼ }else{
17 tail->link=temp;
18 tail=temp;
19 }
20
21 }
22 ▼ void display(){

Input Expected Got

 5 10 20 30 40 50 10 20 30 40 50 
10
20
30
40
50

Passed all tests! 

Finish review
SIET LMS

Programming in C and Data Structures


Quiz navigation
Question 1 Write a program to create a singly linked list.
Correct Warning: these links will not save your answers. Use the next button at
where, the first line of input is the number of nodes to create. second line is the data for each of the nodes. the bottom of the page.
Marked out of 1
10.00 sample input 1

Flag 2
Finish attempt ...
question
10

20
sample output 1

10 20

For example:

Input Result

4 10 20 30 40
10
20
30
40

4 23 24 25 26
23
24
25
26

Answer: (penalty regime: 0 %)

Language c

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

struct Node {
int data;
struct Node* next;
};

void append(struct Node** head_ref, int new_data) {


struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
struct Node* last = *head_ref;
new_node->data = new_data;
new_node->next = NULL;

if (*head_ref == NULL) {
*head_ref = new_node;
return;
}

while (last->next != NULL) {


last = last->next;
}

last->next = new_node;
}

void printList(struct Node* node) {


while (node != NULL) {
printf("%d ", node->data);
node = node->next;
}
printf("\n");
}

int main() {

struct Node* head = NULL;


int num_nodes, data;

scanf("%d", &num_nodes);

for (int i = 0; i < num_nodes; i++) {


scanf("%d", &data);
append(&head, data);
}

printList(head);

return 0;
}

Check

Input Expected Got

 4 10 20 30 40 10 20 30 40 
10
20
30
40

 4 23 24 25 26 23 24 25 26 
23
24
25
26

 4 1 2 3 4 1 2 3 4 
1
2
3
4

Passed all tests! 

Finish attempt ...


SIET LMS

Programming in C and Data Structures


Quiz navigation
Question 1 Write a program to insert a given note in a given position in a singly linked list.
Correct Warning: these links will not save your answers. Use the next button at
For example: the bottom of the page.
Marked out of 1
10.00 Input Result
Flag
1 7 10 20 30 40 Finish attempt ...
question
7

2 10 7 20 30 40
7

9 Invalid position 9
7

Answer: (penalty regime: 0 %)

Language c

Reset answer

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

struct node {
int data;
struct node* link;
};

int insert(struct node** head, int position, int value) {


struct node *temp = malloc(sizeof(struct node));
temp->data = value;
temp->link = NULL;

if (position == 1) {
temp->link = *head;
*head = temp;
return 1;
}

struct node *prev = *head;


for (int i = 1; i < position - 1 && prev != NULL; i++) {
prev = prev->link;
}

if (prev == NULL) {
printf("Invalid position %d",position);
free(temp);
return -1;
}

temp->link = prev->link;
prev->link = temp;
return 1;
}

void init_list(struct node** head) {


int values[] = {10, 20, 30, 40};
int n = sizeof(values) / sizeof(values[0]);

struct node *temp = NULL, *prev = NULL;


for (int i = 0; i < n; i++) {
temp = malloc(sizeof(struct node));
temp->data = values[i];
temp->link = NULL;

if (*head == NULL) {
*head = temp;
} else {
prev->link = temp;
}
prev = temp;
}
}

void print(struct node* head) {


struct node *ptr = head;
while (ptr != NULL) {
printf("%d ", ptr->data);
ptr = ptr->link;
}
printf("\n");
}

int main() {
int position, value;
scanf("%d", &position);
scanf("%d", &value);

struct node *head = NULL;

init_list(&head);
int error;
error = insert(&head, position, value);
if(error == -1){
return 0;
}
print(head);

return 0;
}

Check

Input Expected Got

 1 7 10 20 30 40 7 10 20 30 40 
7

 2 10 7 20 30 40 10 7 20 30 40 
7

 9 Invalid position 9 Invalid position 9 


7

Passed all tests! 

Finish attempt ...


SIET LMS

Programming in C and Data Structures


Quiz navigation
Question 1 Write a program to delete a node from singly linked list.
Correct Warning: these links will not save your answers. Use the next button at
Input: the bottom of the page.
Marked out of 1
n - Number of elements
10.00
<<n elements>>
Flag
k - element to be deleted Finish attempt ...
question
For example:

Input Result

5 10 20 40 50
10 20 30 40 50
30

7 1 2 3 4 5 6
1 2 3 4 5 6 7
7

3 2 3
1 2 3
1

2 30 Not in List
10 20
30

Answer: (penalty regime: 0 %)

Language c

Reset answer

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

struct tag
{
int data;
struct tag *next;
};

struct tag *head = NULL, *tail = NULL;

void add(int value)


{
struct tag *temp = (struct tag*)malloc(sizeof(struct tag));
temp->data = value;
temp->next = NULL;

if (head == NULL) {
head = temp;
tail = temp;
} else {
tail->next = temp;
tail = temp;
}
}

void delete(int value)


{
struct tag *ptr = head;
struct tag *prev = NULL;

while (ptr != NULL && ptr->data != value) {


prev = ptr;
ptr = ptr->next;
}

if (ptr == NULL) {
printf("%d Not in List",value);
exit(1);
}

if (prev == NULL) {
// The node to be deleted is the head
head = head->next;
} else {
prev->next = ptr->next;
}

free(ptr);
}

void display()
{
struct tag *ptr = head;
while (ptr != NULL) {
printf("%d ", ptr->data);
ptr = ptr->next;
}
printf("\n");
}

int main()
{
int i, n, value, del;
scanf("%d", &n);
for (i = 0; i < n; ++i)
{
scanf("%d", &value);
add(value);
}
scanf("%d", &del);
delete(del);
display();

return 0;
}

Check

Input Expected Got

 5 10 20 40 50 10 20 40 50 
10 20 30 40 50
30

 7 1 2 3 4 5 6 1 2 3 4 5 6 
1 2 3 4 5 6 7
7

 3 2 3 2 3 
1 2 3
1

 2 30 Not in List 30 Not in List 


10 20
30

Passed all tests! 

Finish attempt ...


SIET LMS

Programming in C and Data Structures

Started on Friday, 7 June 2024, 8:12 AM Quiz navigation


State Finished 1 2 3 4 5 6 7 8 9
Completed on Friday, 7 June 2024, 8:17 AM
Time taken 4 mins 18 secs
Show one page at a time
Marks 17.00/17.00
Grade 100.00 out of 100.00 Finish review

Question 1 What is the time complexity to count the number of elements in the linked list?
Correct

Mark 2.00 out of A. O(1)


2.00

Flag question B. O(logn)

C. None of the mentioned

D. O( n ) 

Your answer is correct.

Question 2 Which of the following application makes use of a circular linked list?
Correct

Mark 2.00 out of A. Recursive function calls


2.00
B. Implement Hash Tables
Flag question
C. Allocating CPU to resources 

D. Undo operation in a text editor

Your answer is correct.

Question 3 Consider an implementation of unsorted singly linked list. Suppose it has its representation with a head and tail pointer. Given the
Correct representation, which of the following operation can be implemented in O(1) time?
Mark 2.00 out of
i) Insertion at the front of the linked list
2.00
ii) Insertion at the end of the linked list
Flag question
iii) Deletion of the front node of the linked list
iv) Deletion of the last node of the linked list

A. I,II and IV

B. I and III

C. I,II and III 

D. I and II

Your answer is correct.

Question 4 Consider the following definition in c programming language struct node


Correct
{
Mark 2.00 out of
2.00 int data;

Flag question struct node * next;


}

typedef struct node NODE;


NODE *ptr;

A. ptr=(NODE*)malloc(NODE);

B. ptr=(NODE*)malloc(sizeof(NODE*));

C. ptr=(NODE)malloc(sizeof(NODE))

D. ptr=(NODE*)malloc(sizeof(NODE)); 

Your answer is correct.

Question 5 Linked list data structure offers considerable saving in _____________


Correct

Mark 2.00 out of A. Speed Utilization


2.00
B. Space Utilization
Flag question
C. Computational Time

D. Space Utilization and Computational Time 

Your answer is correct.

Question 6 What does the following function do for a given Linked List with first node as head? void fun1(struct node* head)
Correct
{
Mark 2.00 out of
2.00 if(head == NULL)

Flag question return;


fun1(head->next);

printf("%d ", head->data);


}

A. Prints all nodes of linked lists

B. Prints alternate nodes of Linked List

C. Prints alternate nodes in reverse order

D. Prints all nodes of linked list in reverse order 

Your answer is correct.

Question 7 What is the functionality of the following piece of code?


Correct
function display() :-
Mark 2.00 out of
if(size == 0)
2.00
print("underflow");
Flag question
else
Node current = first;
while(current != null)
print(current-> data)
current = current->next

A. display the list excluding top-of-the-stack-element

B. reverse the list excluding top-of-the-stack-element

C. reverse the list

D. display the list 

Your answer is correct.

Question 8 Which of the following is false about a doubly linked list?


Correct

Mark 2.00 out of A. The insertion and deletion of a node take a bit longer
2.00 
B. Implementing a doubly linked list is easier than singly linked list
Flag question
C. We can navigate in both the directions
D. It requires more space than a singly linked list

Your answer is correct.

Question 9
What is 9.3 = 9.3 
Correct

Mark 1.00 out of


1.00 Ignore this question - this was created for trial purpose
Enter answer as 9.3
Flag question

Finish review
SIET LMS

Programming in C and Data Structures


Quiz navigation
Question 1 Write a code for doubly linked list and perform the following operations
Correct Warning: these links will not save your answers. Use the next button at
Add a value (from user) - First node the bottom of the page.
Marked out of 1
Add another value (from user) at the end - Append node
10.00
Add another value (from user) at the end - Append node
Flag
Insert another value (from user) between first and second node Finish attempt ...
question
Delete the third node
display the list

In the below example


10->20->30 is created first
Then 10->40->20->30 40 is inserted between 1 and 2
Then 20 is deleted - 3rd node
Then list is displayed

For example:

Input Result

10 10 40 30
20
30
40

Answer: (penalty regime: 0 %)

Language c

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

struct node {
struct node* prev;
int data;
struct node* next;
};
struct node *head = NULL;

void createnode(int data) {


struct node* temp = malloc(sizeof(struct node));
temp->prev = NULL;
temp->data = data;
temp->next = NULL;
head = temp;
}

void appendatend(int data) {


struct node* ptr = head;
struct node* temp = malloc(sizeof(struct node));
temp->prev = NULL;
temp->data = data;
temp->next = NULL;

if (head == NULL) {
head = temp;
return;
}

while (ptr->next != NULL) {


ptr = ptr->next;
}
ptr->next = temp;
temp->prev = ptr;
}

void insertafter(int data, int position) {


struct node* ptr = head;
struct node* temp = malloc(sizeof(struct node));
temp->prev = NULL;
temp->data = data;
temp->next = NULL;

if (head == NULL) {
printf("List is empty.\n");
return;
}

while (position != 1 && ptr != NULL) {


ptr = ptr->next;
position--;
}

if (ptr == NULL) {
printf("Position out of bounds.\n");
return;
}

temp->next = ptr->next;
temp->prev = ptr;
if (ptr->next != NULL) {
ptr->next->prev = temp;
}
ptr->next = temp;
}

void deletenode(int value) {


struct node* ptr = head;

if (head == NULL) {
printf("List is empty.\n");
return;
}

while (ptr != NULL && ptr->data != value) {


ptr = ptr->next;
}

if (ptr == NULL) {
printf("Node with value %d not found.\n", value);
return;
}

if (ptr->prev != NULL) {
ptr->prev->next = ptr->next;
} else {
head = ptr->next; // Update head if node to be deleted is head
}

if (ptr->next != NULL) {
ptr->next->prev = ptr->prev;
}

free(ptr);
}

void printnode() {
struct node *ptr = head;
while (ptr != NULL) {
printf("%d ", ptr->data);
ptr = ptr->next;
}
printf("\n");
}

int main() {
int a, b, c, d;
scanf("%d %d %d %d", &a, &b, &c, &d);

createnode(a);
appendatend(b);
appendatend(c);
insertafter(d, 1);
deletenode(20); // This should be an existing value to test
printnode();

return 0;
}

Check

Input Expected Got

 10 10 40 30 10 40 30 
20
30
40

Passed all tests! 

Finish attempt ...


SIET LMS

Programming in C and Data Structures


Quiz navigation
Question 1
Given two polynomial numbers represented by a linked list. Develop a program to add the given two polynomials.
Correct Warning: these links will not save your answers. Use the next button at
the bottom of the page.
5x2 + 4x + 2
Marked out of 1
10.00 -5x - 5
Flag
Is presented as Finish attempt ...
question
6
524120
4
-5 1 -5 0

For example:

Input Result

6 5x2 - 1x - 3
5 2 4 1 2 0
4
-5 1 -5 0

6 5x3 + 4x2 + 5x - 3
5 3 4 2 2 0
4
5 1 -5 0

Answer: (penalty regime: 0 %)

Language c

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

typedef struct Node {


int coeff;
int power;
struct Node* next;
struct Node* prev;
} Node;

typedef struct {
Node* head;
Node* tail;
} DoublyLinkedList;

Node* createNode(int coeff, int power) {


Node* newNode = (Node*)malloc(sizeof(Node));
newNode->coeff = coeff;
newNode->power = power;
newNode->next = NULL;
newNode->prev = NULL;
return newNode;
}

void append(DoublyLinkedList* list, int coeff, int power) {


Node* newNode = createNode(coeff, power);
if (list->head == NULL) {
list->head = list->tail = newNode;
} else {
list->tail->next = newNode;
newNode->prev = list->tail;
list->tail = newNode;
}
}

void display(DoublyLinkedList* list) {


Node* current = list->head;
int first = 1; // Flag to manage the first term
while (current != NULL) {
if (current->coeff != 0) {
if (!first && current->coeff > 0)
printf(" + ");
if (current->coeff < 0)
printf(" - ");
if (abs(current->coeff) != 1 || current->power == 0)
printf("%d", abs(current->coeff));
else if (abs(current->coeff) == 1 && current->power != 0)
printf("1");
if (current->power != 0) {
printf("x");
if (current->power > 1)
printf("%d", current->power);
}
first = 0; // Clear the flag after the first term is printed
}
current = current->next;
}
printf("\n");
}

DoublyLinkedList* addPolynomials(DoublyLinkedList* poly1, DoublyLinkedList* poly2) {


DoublyLinkedList* result = (DoublyLinkedList*)malloc(sizeof(DoublyLinkedList));
result->head = result->tail = NULL;

Node* p1 = poly1->head;
Node* p2 = poly2->head;

while (p1 != NULL || p2 != NULL) {


if (p1 == NULL) {
append(result, p2->coeff, p2->power);
p2 = p2->next;
} else if (p2 == NULL) {
append(result, p1->coeff, p1->power);
p1 = p1->next;
} else if (p1->power > p2->power) {
append(result, p1->coeff, p1->power);
p1 = p1->next;
} else if (p1->power < p2->power) {
append(result, p2->coeff, p2->power);
p2 = p2->next;
} else {
int sumCoeff = p1->coeff + p2->coeff;
if (sumCoeff != 0) {
append(result, sumCoeff, p1->power);
}
p1 = p1->next;
p2 = p2->next;
}
}

return result;
}

// Function to create a polynomial from input


DoublyLinkedList* createPolynomial(int count, int* terms) {
DoublyLinkedList* poly = (DoublyLinkedList*)malloc(sizeof(DoublyLinkedList));
poly->head = poly->tail = NULL;
for (int i = 0; i < count * 2; i += 2) {
append(poly, terms[i], terms[i + 1]);
}
return poly;
}

int main() {
int n1, n2;

scanf("%d", &n1);
int* poly1_terms = (int*)malloc(n1 * 2 * sizeof(int));

for (int i = 0; i < n1 ; i++) {


scanf("%d", &poly1_terms[i]);
}

scanf("%d", &n2);
int* poly2_terms = (int*)malloc(n2 * 2 * sizeof(int));

for (int i = 0; i < n2 ; i++) {


scanf("%d", &poly2_terms[i]);
}

DoublyLinkedList* poly1 = createPolynomial(n1, poly1_terms);


DoublyLinkedList* poly2 = createPolynomial(n2, poly2_terms);

DoublyLinkedList* result = addPolynomials(poly1, poly2);

display(result);

free(poly1_terms);
free(poly2_terms);
free(poly1);
free(poly2);
free(result);

return 0;
}

Check

Input Expected Got

 6 5x2 - 1x - 3 5x2 - 1x - 3 
5 2 4 1 2 0
4
-5 1 -5 0

 6 5x3 + 4x2 + 5x - 3 5x3 + 4x2 + 5x - 3 


5 3 4 2 2 0
4
5 1 -5 0

Passed all tests! 

Finish attempt ...


SIET LMS

Programming in C and Data Structures


Quiz navigation
Question 1 Implement push, pop and peek functions using doubly linked list.
Correct Warning: these links will not save your answers. Use the next button at
MAX stack size N the bottom of the page.
Marked out of 1 2
10.00 1 - PUSH
2 - POP
Flag
3 - PEEK Finish attempt ...
question
4 - EXIT

For example:

Input Result

5 Stack underflow
2 PUSH 10 SUCCESSFUL
1 10 PUSH 20 SUCCESSFUL
1 20 PUSH 30 SUCCESSFUL
1 30 PUSH 40 SUCCESSFUL
1 40 PUSH 50 SUCCESSFUL
1 50 Stack overflow
1 60 50
3 POP 50
2 40
3 POP 40
2 30
3 POP 30
2 20
3 POP 20
2 10
3 POP 10
2 STACK EMPTY
3 Stack underflow
2
4

Answer: (penalty regime: 0 %)

Language c

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

struct stack {
struct stack* prev;
int data;
struct stack* next;
};

struct stack* head = NULL;


struct stack* tail = NULL;
int tot_stack_size, stack_size = 0;

void push(int data) {


struct stack* temp = (struct stack*)malloc(sizeof(struct stack));
temp->prev = NULL;
temp->data = data;
temp->next = head;

if (head == NULL && stack_size == 0) {


head = temp;
tail = temp;
stack_size = 1;
printf("PUSH %d SUCCESSFUL\n", data);
} else if (stack_size < tot_stack_size) {
head->prev = temp;
head = temp;
stack_size++;
printf("PUSH %d SUCCESSFUL\n", data);
} else {
printf("Stack overflow\n");
free(temp); // free the allocated memory as it won't be used
}
}

void pop() {
if (stack_size == 0) {
printf("Stack underflow\n");
} else {
printf("POP %d\n", head->data);
struct stack* temp = head;
head = head->next;
if (head != NULL) {
head->prev = NULL;
}
free(temp);
stack_size--;
}
}

void peek() {
if (stack_size == 0) {
printf("STACK EMPTY\n");
} else {
printf("%d\n", head->data);
}
}

int main() {
scanf("%d", &tot_stack_size);

int n;
while (1) {
scanf("%d", &n);
if (n == 1) {
int data;
scanf("%d", &data);
push(data);
} else if (n == 2) {
pop();
} else if (n == 3) {
peek();
} else {
return 0;
}
}
}

Check

Input Expected Got

 5 Stack underflow Stack underflow 


2 PUSH 10 SUCCESSFUL PUSH 10 SUCCESSFUL
1 10 PUSH 20 SUCCESSFUL PUSH 20 SUCCESSFUL
1 20 PUSH 30 SUCCESSFUL PUSH 30 SUCCESSFUL
1 30 PUSH 40 SUCCESSFUL PUSH 40 SUCCESSFUL
1 40 PUSH 50 SUCCESSFUL PUSH 50 SUCCESSFUL
1 50 Stack overflow Stack overflow
1 60 50 50
3 POP 50 POP 50
2 40 40
3 POP 40 POP 40
2 30 30
3 POP 30 POP 30
2 20 20
3 POP 20 POP 20
2 10 10
3 POP 10 POP 10
2 STACK EMPTY STACK EMPTY
3 Stack underflow Stack underflow
2
4

Passed all tests! 

Next page
SIET LMS

Programming in C and Data Structures


Quiz navigation
Question 2 Reverse a given string using stack operation?
Warning: these links will not save your answers. Use the next button at
Correct DO NOT USE STRING FUNCTIONS OR ARRAYS. PUSH POP OPERATION HAS TO BE USED the bottom of the page.
Marked out of 1 2
Student CAN use collections Stack
10.00
For example:
Flag
Finish attempt ...
question
Input Result

nimbus submin

Answer: (penalty regime: 0 %)

Language c

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

struct stack{
int data;
struct stack* next;
};

struct stack* head = NULL;

void push(char data){


struct stack* temp = (struct stack*)malloc(sizeof(struct stack));
temp->data = data;
temp->next = NULL;

if(head == NULL){
head = temp;
}
else{
temp->next = head;
head = temp;
}
}

void pop(){
printf("%c", head->data);

struct stack* temp = head;


head = head->next;

free(temp);

int main(){
char str[100];
scanf("%s", str);

int len = strlen(str);

for(int i=0; i<len; i++){


push(str[i]);
}

for(int i=0; i<len; i++){


pop();
}
}

Check

Input Expected Got

 nimbus submin submin 

 Gallo ollaG ollaG 

 ITVAC CAVTI CAVTI 

Passed all tests! 

Previous page Finish attempt ...


SIET LMS

Programming in C and Data Structures


Quiz navigation
Question 1 Given an array of integers temperatures represents the daily temperatures, return an array answer such that answer[i] is the number of days you have
Warning: these links will not save your answers. Use the next button at
Correct to wait after the ith day to get a warmer temperature. If there is no future day for which this is possible, keep answer[i] == 0 instead. the bottom of the page.
Marked out of 1 2 3
For example:
10.00

Flag Input Result


Finish attempt ...
question
8 1,1,4,2,1,1,0,0
73
74
75
71
69
72
76
73

Answer: (penalty regime: 0 %)

Language c

#include <stdio.h>

int main() {
int a;
scanf("%d", &a);
int arr[100];
for(int i = 0; i < a; i++) {
scanf("%d", &arr[i]);
}

int stack[100];
int top = -1;
int result[100];

for(int i = 0; i < a; i++) {


while(top != -1 && arr[i] > arr[stack[top]]) {
int idx = stack[top--];
result[idx] = i - idx;
}
stack[++top] = i;
}

while(top != -1) {
int idx = stack[top--];
result[idx] = 0;
}

for(int i = 0; i < a; i++) {


if(i == a - 1) {
printf("%d", result[i]);
} else {
printf("%d,", result[i]);
}
}

return 0;
}

Check

Input Expected Got

 8 1,1,4,2,1,1,0,0 1,1,4,2,1,1,0,0 
73
74
75
71
69
72
76
73

 3 1,1,0 1,1,0 
30
60
90

Passed all tests! 

Next page
SIET LMS

Programming in C and Data Structures


Quiz navigation
Question 2
Suppose we have a file system that stores both files and directories. Warning: these links will not save your answers. Use the next button at
Correct the bottom of the page.
You have to give the absolute path 1 2 3
Marked out of
10.00 For example the statement could be
Flag
cd dir/../dir/../dir1/../dir1 Finish attempt ...
question
In the above .. represents the parent directory
So dir/../dir/../dir1 would effectively mean dir1 since dir/.. is same as being in the same directory

For example:

Input Result

cd dir/../dir/../dir/../dir1/dir2 dir1/dir2

cd dir2 dir2

Answer: (penalty regime: 0 %)

Language c

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

char* my_strdup(const char* s) {


char* d = malloc(strlen(s) + 1);
if (d == NULL) return NULL;
strcpy(d, s);
return d;
}

char** splitPath(const char* path, int* count) {


char* pathCopy = my_strdup(path);
char* token = strtok(pathCopy, "/");
char** parts = malloc(sizeof(char*) * 100);
int idx = 0;

while (token != NULL) {


parts[idx++] = my_strdup(token);
token = strtok(NULL, "/");
}

*count = idx;
free(pathCopy);
return parts;
}

void freeSplitPath(char** parts, int count) {


for (int i = 0; i < count; i++) {
free(parts[i]);
}
free(parts);
}

int main() {
char input[200];
fgets(input, sizeof(input), stdin);

input[strcspn(input, "\n")] = 0;

if (strncmp(input, "cd ", 3) == 0) {


memmove(input, input + 3, strlen(input) - 2);
}

int count;
char** parts = splitPath(input, &count);

char* result[100];
int length = 0;

for (int i = 0; i < count; i++) {


if (strcmp(parts[i], "..") == 0) {
if (length > 0) {
length--;
}
} else if (strcmp(parts[i], ".") != 0 && strlen(parts[i]) > 0) {
result[length++] = parts[i];
}
}

if (length == 0) {
printf("/");
} else {
for (int i = 0; i < length; i++) {
if (i != 0) {
printf("/");
}
printf("%s", result[i]);
}
}
printf("\n");

freeSplitPath(parts, count);

return 0;
}

Check

Input Expected Got

 cd dir/../dir/../dir/../dir1/dir2 dir1/dir2 dir1/dir2 

 cd dir2 dir2 dir2 

 cd dir/../dir1/dir2/../../dir1 dir1 dir1 

 cd temp/../test/try/../../test test test 

Passed all tests! 

Previous page Next page


SIET LMS

Programming in C and Data Structures


Quiz navigation
Question 3
Given two strings s and t, return true if they are equal when both are typed into empty text editors. '#' means a backspace character. Warning: these links will not save your answers. Use the next button at
Correct the bottom of the page.
Note that after backspacing an empty text, the text will continue empty. 1 2 3
Marked out of
10.00

Flag
Finish attempt ...
question
For example:

Input Result

ab#c True
ad#c

ab#c False
abc

Answer: (penalty regime: 0 %)

Language c

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

typedef struct Node {


char data;
struct Node* next;
} Node;

Node* createNode(char data) {


Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}

void push(Node** top, char data) {


Node* newNode = createNode(data);
newNode->next = *top;
*top = newNode;
}

char pop(Node** top) {


if (*top == NULL) {
return '\0';
}
Node* temp = *top;
*top = (*top)->next;
char popped = temp->data;
free(temp);
return popped;
}

void stackToString(Node* top, char* result) {


int index = 0;
while (top != NULL) {
result[index++] = top->data;
top = top->next;
}
result[index] = '\0';

for (int i = 0; i < index / 2; i++) {


char temp = result[i];
result[i] = result[index - i - 1];
result[index - i - 1] = temp;
}
}

void processString(const char* str, char* result) {


Node* stack = NULL;

for (int i = 0; str[i] != '\0'; i++) {


if (str[i] == '#') {
pop(&stack);
} else {
push(&stack, str[i]);
}
}

stackToString(stack, result);
}

bool backspaceCompare(char* s, char* t) {


char processedS[200];
char processedT[200];

processString(s, processedS);
processString(t, processedT);

return strcmp(processedS, processedT) == 0;


}

int main() {
char s[200], t[200];

scanf("%s", s);
scanf("%s", t);

if (backspaceCompare(s, t)) {
printf("True\n");
} else {
printf("False\n");
}

return 0;
}

Check

Input Expected Got

 ab#c True True 


ad#c

 ab#c False False 


abc

Passed all tests! 

Previous page Finish attempt ...


SIET LMS

Programming in C and Data Structures


Quiz navigation
Question 1
Construct a program to implement enqueue, dequeue functions using doubly linked list. Warning: these links will not save your answers. Use the next button at
Partially correct the bottom of the page.
Consider the following input value for the corresponding operations. Read inputs till you see 4 (EXIT) 1
Marked out of
10.00 1 - ENQUEUE
2 - DEQUEUE
Flag
3 - DISPLAY Finish attempt ...
question
4 - EXIT

For example:

Input Result

1 20
10 30 40
1
20
2
3
1
30
1
40
2
3
4

Answer: (penalty regime: 0 %)

Language c

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

struct queue{
struct queue* prev;
int data;
struct queue* next;
};

struct queue* head = NULL, *tail = NULL;

void enqueue(int data){


struct queue* temp = (struct queue*)malloc(sizeof(struct queue));
temp->prev = NULL;
temp->data = data;
temp->next = NULL;

if(head == NULL){
head = temp;
tail = temp;
}
else{
temp->prev = tail;
tail->next = temp;
tail = temp;
}
}

void dequeue(){
struct queue* temp = head;
head = head->next;
free(temp);
}

void display(){
struct queue* ptr = head;
while(ptr != NULL){
printf("%d ",ptr->data);
ptr = ptr->next;
}
printf("\n");
}

int main(){
int i=1;
while(i>0){
int n;
scanf("%d",&n);

if(n == 1){
int data;
scanf("%d",&data);
enqueue(data);
}
else if(n == 2){
dequeue();
}
else if(n == 3){
display();
}
else{
return 0;
}
i++;
}
}

Check

Input Expected Got

 1 20 20 
10 30 40 30 40
1
20
2
3
1
30
1
40
2
3
4

Your code failed one or more hidden tests.

Finish attempt ...

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy