0% found this document useful (0 votes)
21 views14 pages

AsympNotn Pointers Strings

The document contains a series of questions and problems related to asymptotic notation, time complexity, and pointers in C programming. It includes tasks such as arranging functions by their growth rates, analyzing code complexities, and understanding pointer behavior. Additionally, it features programming exercises and questions about the output of various C code snippets.

Uploaded by

lalitha vaddadi
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)
21 views14 pages

AsympNotn Pointers Strings

The document contains a series of questions and problems related to asymptotic notation, time complexity, and pointers in C programming. It includes tasks such as arranging functions by their growth rates, analyzing code complexities, and understanding pointer behavior. Additionally, it features programming exercises and questions about the output of various C code snippets.

Uploaded by

lalitha vaddadi
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

Asymptotic Notation

1) Arrange the functions in the increasing order of asymptotic complexity.


f1(n) = 2n
f2(n) = n3/2
f3(n) = nLogn
f4(n) = nLogn

2)Consider the following three functions. f1 = 10n, f2 = nlogn, f3 = n√n Which


one of the following options arranges the functions in the increasing order of
asymptotic growth rate?
(A) f3,f2,f1 (B) f2,f1,f3 (C) f1,f2,f3 (D) f2,f3,f1

3) For the functions nk and cn, what is the asymptotic relationship between
these functions? Assume that k > 1 and c > 1 are constants.

4) Given an integer array of size N, we want to check if the array is sorted (in
either ascending or descending order). An algorithm solves this problem by
making a single pass through the array and comparing each array element only
with its adjacent elements. Analysis of the worst-case time complexity of this
algorithm.

5) What is the complexity of the following code?


sum=0;
for(i=1;i<=n;i*=2)
for(j=1;j<=n;j++)
Sum++;

6) Consider the following C function


int fun (int n) {
int i, j;
for (i = 1; i < = n; i++) {
for (j = 1 ; j < n ; j+=i) {
printf ("%d %d , i, j ) ;
}
}
}

Asymptotic Notation of fun in terms of θ notation.

7) What is the time complexity of the following code:

int a = 0;
for (i = 0; i < N; i++) {
for (j = N; j > i; j--) {
a = a + i + j;
}
}
8) What is the time complexity of the following code:

int i, j, k = 0;
for (i = n / 2; i <= n; i++) {
for (j = 2; j <= n; j = j * 2) {
k = k + n / 2;
}
}
9) Analysis of the time and space complexity of the following code.

int a = 0, b = 0;
for (i = 0; i < N; i++) {
a = a + rand();
}
for (j = 0; j < M; j++) {
b = b + rand();
}

10) Consider the following functions:

f(n) = 2^n
g(n) = n!
h(n) = n^logn
Which of the following statements about the asymptotic behavior of f(n), g(n),
and h(n) is true?
(a) f(n) = O(g(n)); g(n) = O(h(n))
(b) f(n) = omega (g(n)); g(n) = O(h(n))
(c) g(n) = O(f(n)); h(n) = O(f(n))
(d )h(n) = O(f(n)); g(n) = omega (f(n))

11)Given two algorithms with time complexities T1(n)=O(nlogn) and


T2(n)=O(n3/2), analyze their performance for small and large input sizes n. Which
algorithm will perform better as n→∞ and why?

12) Consider two functions, f(n)=n1.5 and g(n)=nlog⁡2n. Determine which


function grows faster as n→∞ and explain your reasoning

13)Derive the time complexity of an algorithm that performs a linear search on


an array of size n and then performs a binary search on the sorted version of the
array.

14)Suppose an algorithm has a worst-case time complexity of T(n)=O(n2) and an


average-case complexity of T(n)=O(nlogn). Explain why it is possible for an
algorithm to have different complexities for the average and worst cases and
provide an example.

15) Consider the following function: f(n)=3n2+2n log⁡ n+10. Determine the Big-
O complexity of f(n).

16)An algorithm’s time complexity is T(n)=O(n2). Can we say the algorithm is


O(n3) as well? Justify your answer.

17)Explain why the function g(n)=2n grows asymptotically faster than any
polynomial function of n.

18)For a function f(n)=n3+5n2+n+10 identify the dominant term and determine


the Big-O notation of f(n).
19)Explain why O(n2)+O(n)=O(n2) and provide an example of an algorithm where
combining complexities yields this result.

20)If the time taken by two programs is T(n) = a*n + b, and V(n) = c*n2 + d*n + e.
Then, which program will be more efficient?

21) Find the upper bound of the running time of a linear function f(n) = 6n + 3

22)Find the upper bound of the running time of quadratic function f(n) = 3n2 +
2n + 4.

23)Find the lower and tight bound of the running time of the following function
i) f(n) = 6n + 3.
ii) f(n) = 3n2 + 2n + 4
iii) f(n) = 2n3 + 4n + 5

Pointers

24) How do you declare a pointer to a function that takes two integers and
returns an integer in C?
a) int(*ptr)(int,int);
b) ptr(int,int) int;
c) function int (*ptr)(int,int);
d) ptr function(int,int) int;

25) The output of the following program is:


#include<stdio.h>
int main(void)
{
int x, *p;
x = 30;
p = x;
printf(“%d”, *p);
return 0;
}

a) 30
b) value of x
c) address of x
d) Error

26) Consider the following C code segment:


int a = 10, b = 20;
int *p = &a, *q = &b;
p = q;
With respect to the code segment mentioned above, which
of the following statements is correct?

a) Both a and b will contain 20


b) Both p and q will point to b
c) Both p and q will point to a
d) Error

27) Consider the following C program:


#include<stdio.h>
int main()
{
int a[ ] = {2,4,6,8,10};
int i, sum = 0, *b = a + 4;
for (i = 0; i < 5; i++)
sum = sum + (*b – i) - *(b – i);
printf(“ %d \n”, sum);
return 0;
}

The output of the above C program is:


a) 10
b) 6
c) 30
d) Error

28) Consider the following C function


#include< stdio.h>
int main()
{
char c[ ] = “ICRBCSIT17”;
char *p = c;
printf(“%s” , c + 2[p] – 6[p] -1);
return 0;
}

The output of the program is


a) SI
b) IT
c) 17
d) Error

29) If X, Y and Z are pointer variables of type char, int and float,
respectively in ‘C’ language, then which of the following statements is true?
a) Size of X, Y and Z are same
b) Size of Z is greater than the size of X
c) Size of Y is greater than the size of X
d) Size of Z is greater than the size of Y

30) Which of the following is the correct syntax to send an array as a


parameter to function?
a) func(&array);
b) func(#array);
c) func(*array);
d) func(array[size])
31) What is the output of this C program?
#include<stdio.h>
void square (int *x)
{
*x = (*x)++ *(*x);
}
void square(int *x, int *y)
{
*x = (*x) * --(*y);
}
int main()
{
int number = 30;
square(&number, &number);
printf(“%d”, number);
return 0;
}

a) 910
b) 920
c) 870
d) 900

32) What does the following statement mean?


int (*fp)(char*)

a) Pointer to a pointer
b) Pointer to an array of chars
c) Pointer to function taking char* argument and returns an int
d) Throws an error

33) What would be the equivalent pointer expression for referring the array
element a[i][j][k][l]
a) ((((a+i)+j)+k)+l)
b) *(*(*(*(a+i)+j)+k)+l)
c) (((a+i)+j)+k+l)
d) ((a+i)+j+k+l)

34) #include <stdio.h>


void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}

int main()
{
int x = 5, y = 10;
swap(&x, &y);
printf("x = %d, y = %d", x, y);
return 0;
}

a) Swaps the values of x and y


b) Prints x = 5, y = 10
c) Prints x = 10, y = 5
d) Results in a compilation error

35) #include <stdio.h>


int main()
{
int arr[] = {2, 4, 6, 8};
int *p = arr + 3;
printf("%d", *(--p));
return 0;
}
a) 2
b) 4
c) 6
d) 8

36) #include <stdio.h>


int main()
{
int arr[] = {1, 2, 3, 4, 5};
int *p = arr + 2;
int *q = arr;
printf("%d", p - q);
return 0;
}

a) 2
b) 3
c) 4
d) Undefined behavior

37) If ptr is a pointer to int, having value ptr = 100. After ptr++, what is the
value of ptr?
a) 100
b) 101
c) 102
d) 103

38) #include <stdio.h>


int main()
{
char *str = "hello";
printf("%c %c", *str, *(str + 4));
return 0;
}

a) ho
b) he
c) h l
d) Undefined behavior

39) Is the NULL pointer same as an uninitialized pointer?


a) True
b) False

40) What does the following declaration mean?


int (*ptr)[10];

a) ptr is an array of pointers to 10 integers


b) ptr is a pointer to an array of 10 integers
c) ptr is an array of 10 integers
d) ptr is a pointer to array

41) What is the output of the following program


int arr[5] = {1,2,3,4,5};
int *ptr = arr;
printf(“%d”, *(ptr++));

a) 1
b) 2
c) 3
d) Error
42) What is the output of the following program:
main()
{
char c[2] = "A" ;
printf ( "\n%c", c[0] ) ;
printf ( "\n%s", c ) ;
}

43) What is the output of the following program:


main()
{
char s[ ] = "Get organised! learn C!!" ;
printf ( "\n%s", &s[2] ) ;
printf ( "\n%s", s ) ;
printf ( "\n%s", &s ) ;
printf ( "\n%c", s[2] ) ;
}

44) What is the output of the following program:


main()
{
char s[ ] = "No two viruses work similarly" ;
int i = 0 ;
while ( s[i] != 0 ) {
printf ( "\n%c %c", s[i], *( s + i ) ) ;
printf ( "\n%c %c", i[s], *( i + s ) ) ;
i++ ;
}
}

45) What is the output of the following program:


main()
{
printf ( 5 + "Good Morning " ) ;
}
46) What is the output of the following program:
main()
{
printf ( "%c", "abcdefgh"[4] ) ;
}

47) What is the output of the following program:


main()
{
printf ( "\n%d%d", sizeof ( ‘3’ ), sizeof ( "3" ), sizeof ( 3 ) ) ;
}

48) Program to find string length without function in C

49) Program to check if a given string is a palindrome.

50) Program to count character occurrent in C

51) Program to find the first occurrence of a character in a string

52) Program to concatenate two strings in C

53) Program to copy string in C

54) Program to count vowels occurrent in C

55) Program to sort string characters in C

56) Program to copy string in C


57) What is the output of the following C Code?
#include<stdio.h>
int main()
{
char str1[] = "GeeksQuiz";
char str2[] = {'G', 'e', 'e', 'k', 's', 'Q', 'u', 'i', 'z'};
int n1 = sizeof(str1)/sizeof(str1[0]);
int n2 = sizeof(str2)/sizeof(str2[0]);
printf("n1 = %d, n2 = %d", n1, n2);
return 0;
}

a) n1 = 10, n2 = 9
b) n1 = 10, n2 = 10
c) n1 = 9 , n2 = 9
d) n1 = 9 , n2 = 10

58) Considering the size of char(character) variables as one byte,


what will be the size of the array declared below:

char array[]= “programming language”;


a) 11 Bytes
b) 8 Bytes
c) 20 Bytes
d) 21 Bytes

59) What is the output of the code given below?


#include<stdio.h>
int main()
{
char name [] =”satellites”;
int len;
int size;
len= strlen(name);
size= sizeof(name);
printf(“%d”, len*size);
return 0;
}
a) 100
b) 110
c) 40
d) 44

60) Program to find last occurrence of a character in a string


Example: Learn these concepts from the textbook
Character to find: ‘o’
Output: The last occurrence of 'o' is at index 36.

You might also like