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

Arrays&Strings Quiz Solution

The document provides a series of programming problems and their solutions related to arrays and strings in C. Each problem includes an explanation of the code, expected output, and reasoning behind the results. Topics covered include string manipulation, memory addresses, array sizes, and function usage.

Uploaded by

alis1a2y3ed
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)
1 views

Arrays&Strings Quiz Solution

The document provides a series of programming problems and their solutions related to arrays and strings in C. Each problem includes an explanation of the code, expected output, and reasoning behind the results. Topics covered include string manipulation, memory addresses, array sizes, and function usage.

Uploaded by

alis1a2y3ed
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/ 14

Prepared by Eng.Mohamed Gamal Email:mohamedgamal1999993@gmail.

com
3)Arrays and Strings
1)

Output :Hello World.


Explanation: Step 1: char str1[20] = "Hello", str2[20] = " World"; The variable str1 and str2 is declared as an array of
.characters and initialized with value "Hello" and " World" respectively
;)))Step 2: printf("%s\n", strcpy(str2, strcat(str1, str2
strcat(str1, str2)) it append the string str2 to str1. The result will be stored in str1. Therefore str1 contains "Hello >=
."World
.strcpy(str2, "Hello World") it copies the "Hello World" to the variable str2 >=
"Hence it prints "Hello World
……………………………………………………………………………………………………………………………………………………….
2)
which of the following function is used to find the first occurrence of a given string in
another string?
Output: strstr().
Explanation: Declaration: char *strstr(const char *s1, const char *s2);
int main(void)
{
char *str1 = "Mohamed", *str2 = "am", *ptr;
ptr = strstr(str1, str2);
printf("The substring is: %s\n", ptr);
return 0;
}
……………………………………………………………………………………………………………………………………………………….
3) What will be the output of the program if the array begins at 65472 and each integer
occupies 2 bytes?
Output:65480 65496.
Explanation: For a two-dimensional array like a reference to array has type "pointer to array of
4 ints". Therefore, a+1 is pointing to the memory location of first element of the second row in
array a. Hence 65472 + (4 ints * 2 bytes) = 65480
Then, &a has type "pointer to array of 3 arrays of 4 ints", totally 12 ints. Therefore, &a+1
denotes "12 ints * 2 bytes * 1 = 24 bytes".
…………………………………………………………………………………………………………………………………..
4)

Output:20.
Explanation:Notice size between brackets of array.

…………………………………………………………………………………………………………………………………..
5) assume double=8 byte ,int=2 byte,char=1byte .

Output:4.
Explanation:!a will convert to int so int+char=int(2 Bytes), so size of array is 2*2=4Bytes.
…………………………………………………………………………………………………………………………………..
6)

Output:Morning.
Explanation:it skips the first 5 characters and print the given string.
…………………………………………………………………………………………………………………………………..
7)

Output: Suresh, Siva, Sona, Ritu, Baiju.


Explanation:
Step 1: char *names[] = { "Suresh", "Siva", "Sona", "Baiju", "Ritu"}; The
.variable names is declared as an pointer to a array of strings
.Step 2: int i; The variable i is declared as an integer type
.Step 3: char *t; The variable t is declared as pointer to a string
Step 4: t = names[3]; names[3] = names[4]; names[4] = t; These statements the
.swaps the 4 and 5 element of the array names
Step 5: for(i=0; i<=4; i++) printf("%s,", names[i]); These statement prints
.the all the value of the array names
"Hence the output of the program is "Suresh, Siva, Sona, Ritu, Baiju
…………………………………………………………………………………………………………………………………..
8) What will be the output of the program if the array begins 1200 in memory? *

Output:1200 1200 1200.


Explanation:
Step 1: int arr[]={2, 3, 4, 1, 6}; The variable arr is declared as an integer
.array and initialized
,Step 2: printf("%u, %u, %u\n", arr, &arr[0], &arr); Here
.The base address of the array is 1200
.arr, &arr is pointing to the base address of the array arr >=
arr[0] is pointing to the address of the first element array arr. (ie. base & >=
)address
Hence the output of the program is 1200, 1200, 1200
…………………………………………………………………………………………………………………………………..
9)

Output:6.
Explanation: count number of characters until null terminator.

…………………………………………………………………………………………………………………………………..
10)

Output: 0
Explanation: if two strings is equal so strcmp return zero.
…………………………………………………………………………………………………………………………………..
11)

Output:The sentence will get printed in reverse order


Explanation: 'i'is initialized with the last element in the string ad
decrement till reach the first character in string.
…………………………………………………………………………………………………………………………………..
12)

Output: Garbage value.


Explanation: Step 1: char str[25] = "IndiaBIX"; The variable str is declared as
."an array of characteres and initialized with a string "IndiaBIX
;)Step 2: printf("%s\n", &str+2
In the printf statement %s is string format specifier tells the compiler to >=
print the string in the memory of &str+2
str is a location of string "IndiaBIX". Therefore &str+2 is another memory & >=
.location
.Hence it prints the Garbage value
…………………………………………………………………………………………………………………………………..
13)

Output:5
Explanation:
The function strlen returns the number of characters int the given string
Therefore, strlen(str) becomes strlen("India") contains 5 characters. A string
'is a collection of characters terminated by '\0
"The output of the program is "5
…………………………………………………………………………………………………………………………………..
14)

Output: Compilation error.


Explanation: we must use constant size for array.
15)

Which of the following function is more appropriate for reading in a multi-word string?
Output: gets().
Explanation: to read multi-word we use gets() or fgets(), fgets is safer than gets.
…………………………………………………………………………………………………………………………………..

16)

What will be the output of the program ?

Output:3,2,15.
Explanation: Step 1: int a[5] = {5, 1, 15, 20, 25}; The variable arr is declared
as an integer array with a size of 5 and it is initialized to
. a[0] = 5, a[1] = 1, a[2] = 15, a[3] = 20, a[4] = 25
.Step 2: int i, j, m; The variable i,j,m are declared as an integer type
Step 3: i = ++a[1]; becomes i = ++1; Hence i = 2 and a[1] = 2
.Step 4: j = a[1]++; becomes j = 2++; Hence j = 2 and a[1] = 3
Step 5: m = a[i++]; becomes m = a[2]; Hence m = 15 and i is incremented by
)1(i++ means 2++ so i=3
Step 6: printf("%d, %d, %d", i, j, m); It prints the value of the variables i,
j, m
Hence the output of the program is 3, 2, 15
…………………………………………………………………………………………………………………………………..

17)

Output: 10.
Explanation:0[arr]=arr[0].(later in pointer).
18)

What will be the output of the following program in 16 bit platform assuming that 1022 is
memory address of the string "Hello1" ?

Output: 1022 Hello2.


Explanation: In printf("%u %s\n", &"Hello", &"Hello");.
The %u format specifier tells the compiler to print the memory address of the
"Hello1".
The %s format specifier tells the compiler to print the string "Hello2".
Hence the output of the program is "1022 Hello2".
…………………………………………………………………………………………………………………………………..
19)

Output: A.
Explanation: Step 1: char p[] = "%d\n"; The variable p is declared as an array
of characters and initialized with string "%d"
Step 2: p[1] = 'c'; Here, we overwrite the second element of array p by 'c'.
So array p becomes "%c"
Step 3: printf(p, 65); becomes printf("%c", 65),Therefore it prints the ASCII
'value of 65. The output is 'A
…………………………………………………………………………………………………………………………………..
20)

Output :India.
Explanation:str will print till found null terminated('\0').
21)

Output: Warning and print Garbage value.


Explanation: Initializer string for array is so long.
…………………………………………………………………………………………………………………………………..
22)

How will you print \n on the screen? *


Printf("\\n");
…………………………………………………………………………………………………………………………………..
23)

Output: Unequal.
Explanation: Step 1: char str1[] = "Hello"; The variable str1 is declared as an array of characters
.and initialized with a string "Hello"
Step 2: char str2[] = "Hello"; The variable str2 is declared as an array of characters and
initialized with a string "Hello"
.We have use strcmp(s1,s2) function to compare strings
Step 3: if(str1 == str2) here the address of str1 and str2 are compared. The address of both
.variable is not same. Hence the if condition is failed
Step 4: At the else part it prints "Unequal"
………………………………………………………………………………………………………………………………….
24)

Output: The program may crash if some important data gets overwritten .
Explanation: If the index of the array size is exceeded, the program will crash. Hence "option c" is
the correct answer. But the modern compilers will take care of this kind of errors .
Since C is a compiler dependent language, it may give different outputs at
.different platforms. We have given the Turbo-C Compiler (Windows) output
Please try the above programs in Windows (Turbo-C Compiler) and Linux (GCC
.Compiler), you will understand the difference better
………………………………………………………………………………………………………………………………….
25)Assume that int is 2 Bytes.

Output: 18 0 5.
Explanation: we have 3 rows and 3 cols so array size is 3*3*sizeof(int)=18,
arr[0][2]=0(the compiler put it) and arr[1][2]=5.
………………………………………………………………………………………………………………………………….
26)

Output: Garbage Value.


Explanation:As size of array is 7 and number of elements is 8.
………………………………………………………………………………………………………………………………….
27)

Output:3 8 4 9.

………………………………………………………………………………………………………………………………….
28)

Output: Compilation error.


Explanation: to define array we must put data type of array, so we can't write
arr[20].
………………………………………………………………………………………………………………………………….
29)

If the two strings are identical, then strcmp() function returns ?


Output:0.
Explanation: strcmp returns zero if two strings are identical, returns '1' if str1>str2. And returns
'-1' if str1<str2.
………………………………………………………………………………………………………………………………….
30)

Output:9.
Explanation:(buff+1)[5]=buff[6](later in pointers).
………………………………………………………………………………………………………………………………….
31)

Output:Sequential.
………………………………………………………………………………………………………………………………….
32)

Output:India.
Explanation: printf("India", "BIX\n"); It prints "India". Because ,(comma) operator has
Left to Right associativity. After printing "India", the statement got terminated .
33)

The library function used to find the last occurrence of a character in a string is ?
Output: strrchr().
Explanation: Declaration: char *strrchr(const char *s, int c);
int main(void)

char text[] = "I learn through IndiaBIX.com";

char *ptr, c = 'i';

ptr = strrchr(text, c);

if (ptr)

printf("The position of '%c' is: %d\n", c, ptr-text);

else

printf("The character was not found\n");

return 0;

………………………………………………………………………………………………………………………………….
34)

n C, if you pass an array as an argument to a function, what actually gets


passed?

Output:Base address of the array.


Explanation: Later in pointers(:.D)
………………………………………………………………………………………………………………………………….
35)

Output:5.
36)

Output:e.
Explanation: consider str="abcdefgh", and we want to print str[4], so will print
e.
………………………………………………………………………………………………………………………………….
37)

Output:The string is empty.


Explanation:printf will return zero so else will execute.
………………………………………………………………………………………………………………………………….
38)

Output: 0 0 \n 1 1 \n 2 2 \n 3 3\n 4 4.
Explanation: comma here is separator so condition will be b<5.
39)

Output: Answer A.

………………………………………………………………………………………………………………………………….

40)

Output: 0 0.
Explanation:the compiler will initialize the rest of elements
by zeros.

You might also like