CS 261 Data Structures
questions(solved & verified for
accuracy)
What will be the output?
int arr[] = {1,2,3,4,5);
int *ptr = arr;
*ptr = 10;
printf("Output 1: %d \n", arr[0]);
printf("Output 2: %d \n", *(ptr+2)); - answer Output 1: 10
Output 2: 3
Output?
int total = 0;
For(int i = 1; i <= 6; i++){
if(i%2 == 0){
total += i;
printf("total = %d \n", total);
}
} - answer total = 2
total = 4
total = 6
total = 12
, What is a dynamic array in C? - answer An array whose size can be
changed at runtime
Value of ptr after memory allocation?
int *ptr;
ptr = (int*)malloc(sizeof(int) *5); - answer Address of first element.
sizeof(int) makes 4 bytes, times 5 equals 20 bytes. int* before
malloc specifies the pointer returned by malloc will point to an
integer.
Which header file do you include to use to include the malloc
function in C? - answer <stdlib.h>
How would you write a struct with two member int and char? -
answer struct MyStruct{ int a; char b;};
What is printed?
int a=10, b=20, c=30;
int *array[3] = {&a, &b, &c};
printf("%d", *array[2]); - answer 30
Output?
int *arr = (int*)malloc(6*sizeof(int));
for(int i =0; i < 6;i++){
arr[i] = i * 2;
}
questions(solved & verified for
accuracy)
What will be the output?
int arr[] = {1,2,3,4,5);
int *ptr = arr;
*ptr = 10;
printf("Output 1: %d \n", arr[0]);
printf("Output 2: %d \n", *(ptr+2)); - answer Output 1: 10
Output 2: 3
Output?
int total = 0;
For(int i = 1; i <= 6; i++){
if(i%2 == 0){
total += i;
printf("total = %d \n", total);
}
} - answer total = 2
total = 4
total = 6
total = 12
, What is a dynamic array in C? - answer An array whose size can be
changed at runtime
Value of ptr after memory allocation?
int *ptr;
ptr = (int*)malloc(sizeof(int) *5); - answer Address of first element.
sizeof(int) makes 4 bytes, times 5 equals 20 bytes. int* before
malloc specifies the pointer returned by malloc will point to an
integer.
Which header file do you include to use to include the malloc
function in C? - answer <stdlib.h>
How would you write a struct with two member int and char? -
answer struct MyStruct{ int a; char b;};
What is printed?
int a=10, b=20, c=30;
int *array[3] = {&a, &b, &c};
printf("%d", *array[2]); - answer 30
Output?
int *arr = (int*)malloc(6*sizeof(int));
for(int i =0; i < 6;i++){
arr[i] = i * 2;
}