0% found this document useful (0 votes)
11 views

CIE-2 Study Material

The document provides an overview of arrays and strings in C programming, detailing their declaration, initialization, and access methods. It includes multiple-choice questions (MCQs) to test understanding of array operations, memory allocation, and string handling. Key concepts such as array indexing, string termination with null characters, and common string functions are also discussed.

Uploaded by

mp4248728
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)
11 views

CIE-2 Study Material

The document provides an overview of arrays and strings in C programming, detailing their declaration, initialization, and access methods. It includes multiple-choice questions (MCQs) to test understanding of array operations, memory allocation, and string handling. Key concepts such as array indexing, string termination with null characters, and common string functions are also discussed.

Uploaded by

mp4248728
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/ 17

1

Arrays
Array in C is one of the most used data structures in C programming. Arrays are used to store
multiple values in a single variable, instead of declaring separate variables for each value.

Array Declaration
In C, we have to declare the array like any other variable before using it. We can declare an array
by specifying its name, the type of its elements, and the size of its dimensions. When we declare
an array in C, the compiler allocates the memory block of the specified size to the array name.
Syntax:
data_type arrayname[size]; or data_type arrayname [size1][size2]….[sizeN];

Access Array Elements:


2

We can access any element of an array in C using the array subscript operator [ ] and the index
value i of the element.
Array_name[index]

MCQ
Pseudocodes on Arrays:
1. What is the right way to initialize an array?
a. int num[6] = { 2, 4, 12, 5, 45, 5 }; c. int n{} = { 2, 4, 12, 5, 45, 5 };
b. int n{6} = { 2, 4, 12 }; d. int n(6) = { 2, 4, 12, 5, 45, 5 };
2. What will be the output of the program ?
#include <stdio.h>
void main()
{
int a[5] = { 5,1,15, 20,25},i,j,m;
i = ++a[1];
j = a[1]++;
m= a[i++];
printf(“%d%d%d”, i, j ,m);
}
a. 3,2,15 b. 2,3, 20 c. 2,1,15 d. 1,2,5
3. What will be the output of the program ?
#include <stdio.h>
void main()
{
char p;
3

char buf[10] = {1,2,3,4,5,6,9,8}


p = (buf +1) [5];
printf(“%d”, p);
}
b. 5 b. 6 c. 9 d. Error e. None of the above
4. An array elements are always stored in _____memory locations.
a. Sequential b. Random c. Sequential and Random d. None of the above
5. Let x be an array. Which of the following operations are illegal?
i. ++x ii. x+1 iii. x++ iv. x*2
a. i and ii b. i ,ii and iii c. ii and iii d. i, iii and iv e. iii and iv
6. Size of the array need not be specified , when
a. Initialization is a part of definition c. It is a declaration
b. It is a formal parameter d. All of these
7. Consider the following type definition
Typedef char x[10]
X myArray[5]
What will be the size of (myArray)be?
a. 15 b. 10 c. 50 d. 30 e. None of these
8. What will be printed after execution of the following code?
#include <stdio.h>
void main()
{
int a[10] = {1,2,3,4,5}
printf(“%d”,a[5]);
}
a. Garbage value b. 5 c. 6 d. 0 e. None of these
9. What will be the output of the following program?
void main()
{
char str1[] = “abcd”
char str2[] = “abcd”
if(str1 == str2)
printf(“Equal”);
else
printf(“Unequal”);
}
a. Equal b. Unequal c. Error d. None of these
10. What will be the output of the following code?
void main()
{
4

int a[10];
printf(“%d%d”,a[-1],a[12]);
}
a. 0 0 b. Garbage value 0 c. 0 Garbagevalue d. GarbageValue
GarbageValue
11. What will be the output of the program if the array begins at address 65486?
#include<stdio.h>
void main()
{
int a[] = {12,14,15,23, 45}
printf(“%u%u”, a, &a);
}
a. 65486, 65488 b. 65486, 65490 c. 65486, 65487 d. 65486, 65486
12. What will be the output of the program?
#include<stdio.h>
void main()
{
float a[] = {12.4,2.3,4.5,6.7}
printf(“%d”, sizeof(a)/sizeof(a[0]));
}
a. 5 b. 4 c. 6 d. 7 e. None of these

13. Which of the following is the correct syntax to access the 3rd element of an array arr?
a. arr[2] b. arr(3) c. arr.2 d. arr{3}
14. In C, arrays are _ indexed.
a. 1 b. 2 c. 0 d. Negative
15. What does the following code print?
#include <stdio.h>
int main()
{
int arr[3] = {5, 10, 15};
printf("%d", arr[1]);
}
a. 5 b. 10 c. 15 d. Error

16. Which of the following statements about arrays in C is correct?


a. The size of an array must be determined at runtime.
b. Arrays in C can have a dynamic size.
c. The size of an array must be a constant expression.
d. Arrays in C are always passed by reference.
5

17. What will happen if you try to access an array element out of bounds?
a. The program will crash.
b. The compiler will catch the error.
c. The program will return garbage values.
d. The program will display a warning at runtime
18. What is the size of arr in the following code if int arr[10]; assuming int is 4 bytes?
a. 10 bytes b. 40 bytes c. 10 d. 4 bytes
19. What is the index of the last element in an array of size n in C?
a. n b. n-1 c. n+1 d. 0
20. What are the elements present in the array of the following int array[5] = {5};
a. 5, 5, 5, 5, 5 c. 5, (garbage), (garbage), (garbage), (garbage)
b. 5, 0, 0, 0, 0 d. (garbage), (garbage), (garbage), (garbage), 5
21. If integer requires 2bytes space, then what will be the size of the array int array[3][4]=(0);
a. 24 bytes b. 12 bytes c. 7 bytes d. 14 bytes
22. Consider the following declaration of an array int A[6] = {1,2,3,5}. Which of the
following statement is true?
a. Both A[2] and 2[A] represent the value 2
b. Both A[2] and 2[A] represent the value 3
c. A[2] is the correct way to access an element of A, but 2[A] will give an error.
d. The last two elements of the array A are 3 and 5
23. Consider the following array declaration:
int array1[]={2,3};
int array2[3]={9}
What will be the output of the following print statement?
printf(“%d%d”, array1[1],2[array2]);
a. 2,0 b. 3 0 c. 2 9 d. 3 9
24. What purpose does the following code serves
int main()
{
int array[] = {5, 3, 1, 9, 8, 2, 4, 7};
int size = sizeof(array)/sizeof(array[0]);
int i, j, min_idx,temp;
for (i = 0; i < size-1; i++)
{
min_idx = i;
for (j = i+1; j < size; j++)
{
if (array[j] < array[min_idx])
min_idx = j;
6

}
temp = array[min_idx];
array[min_idx] = array[i];
array[i] = temp; }
}
a. Finds some specific elements in the array c. Sort the elements of an
array
b. Find the smallest element in the array d. None of the above
25. What will be output of the following program
int main()
{
int arr[4]={3,4,5,6};
int k[4];
k=arr;
printf(“%d\n”,k[1]);
}

a) Compile Time Error


b) 4
c) No output
d) Program crashes
Strings
Strings are used for storing text/characters.
For example, "Hello World" is a string of characters.
Unlike many other programming languages, C does not have a String type to easily create
string variables. Instead, you must use the char type and create an array of characters to
make a string in C:
A String in C programming is a sequence of characters terminated with a null character ‘\0’.
The C String is stored as an array of characters. The difference between a character array and
a C string is that the string in C is terminated with a unique character ‘\0’.
char greetings[] = "Hello World!";
C String Initialization:
1. Assigning a String literal without size:
String literals can be assigned without size. Here, the name of the string str acts as a
pointer because it is an array.
char str[] = “Hello World”;
7

2. Assigning a String literal with a predefined size:


String literals can be assigned with a predefined size. But we should always account for
one extra space which will be assigned to the null character. If we want to store a string
of size n then we should always declare a string with a size equal to or greater than n+1.
char str[50] = “Hello World”;
3. Assigning Character by Character with size
We can also assign a string character by character. But we should remember to set the end
character as ‘\0’ which is a null character.
char str[12] = {‘H’,’e’,’l’,’l’,’o’,’W’,’o’,’r’,’l’,’d’,’\0’};
4. Assigning Character by Character without size
We can assign character by character without size with the NULL character at the end.
The size of the string is determined by the compiler automatically.
char str[] = {‘H’,’e’,’l’,’l’,’o’,’W’,’o’,’r’,’l’,’d’,’\0’};

String functions:

MCQ
1. What is string in c language?
a. String is a new Data Type in C
8

b. String is an array of Characters with null character as the last element of


array.
c. String is an array of Characters with null character as the first element of array
d. String is an array of Integers with 0 as the last element of array.
2. Choose a correct statement about C String char ary[] =”Hello..!”
a. Character array, ary is a string.
b. ary has no Null character at the end
c. String size is not mentioned
d. String can not contain special characters.
3. What is the output of the following code?
int main()
{
char ary [] = “Discovery Channel”;
printf(“%s”, ary);
return 0;
}
a. D b. Discovery Channel c.Discovery d. Compiler error
4. What is the output of the following code?
int main()
{
char str [] = {‘g’,’l’,’o’,’b’,’e’};
printf(“%s”, str);
return 0;
}
a. g b. globe c.globe\0 d. None of the above
5. If the two strings are identical , then strcmp() function returns
a. 1 b. 0 c. -1 d. true e. None of these
6. Which of the following function is more appropriate for reading in a multi-word string?
a. scanf() b. gets() c. printf() d. puts() e. None of these
7. What will be the output of the following code?
#include<stdio.h>
#include<string.h>
void main()
{
char str1[20]= “Hello”, str2[20]=”World”;
printf(“%s”, strcpy(str2, strcat(str1,str2));
}
a. Hello World b. World c. WorldHello d. Hello e. None of
these
8. What will be the output of the program?
9

#include<stdio.h>
void main()
{
printf(5+”Good Morning”);
}
a. Good Morning b. M c. Good d. Morning e. None of these
9. What will be the output of the program?
#include<stdio.h>
void main()
{
char str[]=”Exam\0Veda”;
printf(”%s”, str);
}
a. Exam b. Exam Veda c. Exam\0Veda d. Vedae. None of
these
10. The library function used to find the last occurrence of a character in a string is
a. strrchr() b. strstr() c. strnstr() d. laststr()
11. What is the output of the following code?
int main()
{
int str [] = {‘g’,’l’,’o’,’b’,’e’};
printf(“A%c”, str);
printf(“A%s”, str);
printf(“A%c”, str[0]);
return 0;
}
a. AAA b. A Ag Ag c. A*randomchar* Ag Ag d. compile
error
12. What is the output of the following code?
int main()
{
char str1[] = “JOHN”;
char str2[20] ;
str2=str1;
printf(“%s”, str2);
return 0;
}
a. JOHN b. J c. JOHN\0 d. Compile Error
13. What is the output of the code?
10

char str[] = "C Programming";


printf("%c", str[2]);
a. C b. P c. r d. Space
14. Which function is used to get the length of a string in C?
a. strlen() b. strlength() c.length() d. stringlen()
15. What will happen if you try to modify a string literal in C?
a. The program will crash.
b. The string literal will be modified.
c. The compiler will throw an error.
d. The program will display a warning at compile time.
16. What does the following code print?
char str[] = "Hello";
str[0] = 'J';
printf("%s", str);
a. Hello b. Jello c. J d. Error

17. How are strings terminated in C?


a. With a null character '\0' c. With a space character ' '
b. With a semicolon ; d. With a newline character '\n'
18. Predict the output of the following code:
char str[] = "Hello";
printf("%s", str + 2);
a. llo b. ello c. Hello d. He

19. What is the return type of strlen() function in C?


a. int b. char c. char* d. size_t

20. How do you accept a Multi Word Input in C Language?


a. scanf() b.gets() c.getc() d. finds()
Aptitude:
Q1.1: Pooja and Meera together can finish a task in 48 days, while Pooja alone can do it in 72 days. How
long will Meera take to complete the task alone?

A. 96 days

B. 144 days

C. 120 days

D. 80 days

Answer: B. 144 days


11

Q1.2: Ajay and Vijay together can complete a job in 40 days, while Ajay alone can finish it in 60 days.
How many days will Vijay take to do the job alone?

A. 120 days

B. 100 days

C. 80 days

D. 90 days

Answer: A. 120 days

Q2.1: Rohit and Sohan can complete a work in 10 and 15 days, respectively. They work together for 5
days, and then Rohit leaves. How many more days will Sohan take to complete the remaining work?

A. 2 days

B. 3 days

C. 2.5 days

D. 5 days

Answer: C. 2.5 days

Q2.2: Raj and Simran can finish a task in 8 and 12 days, respectively. They work together for 4 days, and
then Raj leaves. How many more days will Simran need to finish the remaining task?

A. 2 days

B. 3 days

C. 1.5 days

D. 2.5 days

Answer: A. 2 days

Q3.1: 6 men or 9 women can finish a job in 12 days. How many men must work with 18 women to
complete the job in 4 days?

A. 5 men

B. 6 men

C. 8 men

D. 7 men

Answer: B 6 men
12

Q3.2: 3 men or 6 women can finish a task in 10 days. How many men should work with 12 women to
complete the work in 3 days?

A. 4 men

B. 3 men

C. 2 men

D. 5 men

Answer: A. 4 men

Q4.1: 30 men can complete a task in 24 days. After 9 days, 20 more men join them. How many more
days will it take to finish the work?

A. 12 days

B. 10 days

C. 14 days

D. 9 days

Answer: D. 9 days

Q4.2: 60 workers can complete a job in 20 days. After 5 days, 40 additional workers join them. How many
days will it take to complete the remaining work?

A. 10 days

B. 8 days

C. 9 days

D. 7 days

Answer: C. 9 days

Q5.1: How many 3-digit odd numbers (with distinct digits) can you form using 0, 2, 3, 4, 5, 6?

A. 48

B. 60

C. 72

D. 32

Answer: D. 32

Q5.2: How many 4-digit even numbers (with distinct digits) can be formed using 1, 2, 3, 4, 5, 6?
13

A. 120

B. 180

C. 168

D. 192

Answer: B. 180

Q6.1: How many 6-letter words can be formed using the letters in "BANANA" if each letter can be used
only once?

A. 60

B. 420

C. 210

D. 120

Answer: A. 60

Q6.2: How many 7-letter words can be formed using the letters in "BALLOON" if each letter can be used
only once?

A. 420

B. 840

C. 5040

D. 1260

Answer: D. 1260

Q7.1: 8 people are to sit in a row. In how many ways can this be done if 3 specific people always sit
together?

A. 120,960

B. 720

C. 4320

D. 60,480

Answer: C. 4320

Q7.2: 5 men and 4 women are to sit in a row. In how many ways can they be seated if all women must sit
together?
14

A. 17,280

B. 2,880

C. 362880

D. 12,960

Answer: A. 17280

Q8.1: The mean of 40 observations was 55. Later, it was found that an observation 75 was wrongly
recorded as 25. What is the correct mean?

A. 56.25

B. 55.75

C. 55.50

D. 56.00

Answer: A. 56.25

Q8.2: The mean of 25 observations was 64. It was later discovered that an observation recorded as 50
should have been 80. What is the corrected mean?

A. 65.2

B. 64.8

C. 66.0

D. 65.6

Answer: A. 65.2

Q9.1: A batsman has an average of 50 runs after 6 innings. After his seventh inning, the average
decreases to 48. How many runs did he score in the seventh inning?

A. 30

B. 36

C. 40

D. 44

Answer: B. 36

Q9.2: A cricketer has an average of 42 runs after 8 innings. In the ninth inning, his average drops to 40.
How many runs did he score in the ninth inning?
15

A. 24

B. 28

C. 30

D. 32

Answer: A. 24

Q10.1: The average age of 30 people is 40 years. 10 new people join, and their average age is 35 years.
What is the average age of the group now?

A. 38.75

B. 39.0

C. 39.5

D. 40.5

Answer: A. 38.75

Q10.2: The average age of a group of 20 people is 25 years. If 5 new members with an average age of 30
years join, what is the average age of the new group?

A. 27

B. 26

C. 28

D. 29

Answer: B. 26

Q11.1: Two sugar solutions, A (30%) and B (50%), are mixed in the ratio 3:2. What is the concentration of
the resulting solution?

A. 38%

B. 40%

C. 42%

D. 45%

Answer: A. 38%

Q11.2: Two mixtures of alcohol and water have concentrations of 70% and 40%, respectively. If they are
mixed in the ratio 1:2, what is the concentration of alcohol in the final mixture?
16

A. 45%

B. 55%

C. 60%

D. 50%

Answer: D. 50%

Q12.1: A 40-liter solution contains 8% sugar. Another solution with 4% sugar is added. How many liters
of the second solution should be added to make the mixture 5% sugar?

A. 40 liters

B. 50 liters

C. 120 liters

D. 80 liters

Answer: C. 120 liters

Q12.2: A 60-liter solution has 10% alcohol. Another solution with 5% alcohol is added. How many liters
of the second solution should be added to create a mixture with 8% alcohol?

A. 30 liters

B. 40 liters

C. 50 liters

D. 60 liters

Answer: B. 40 liters

Q13.1: Milk and water are in the ratio 3:2 in one container and 5:4 in another. In what ratio should they
be mixed to get a mixture containing 7:6 milk and water?

A. 2:3

B. 3:4

C. 4:5

D. 5:18

Answer: D. 5:18

Q13.2: Two containers have milk and water in the ratios 7:5 and 3:2, respectively. In what ratio should
these mixtures be combined to get a final mixture with milk and water in the ratio 5:4?
17

A. 4:3

B. 3:2

C. 5:4

D. 8:5

Answer: D. 8:5

You might also like