Nothing Special   »   [go: up one dir, main page]

EGN3211.Chapter.7.Non Graded Questions

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 23

Sources / sites

http://www.cquestions.com/2012/02/data-type-questions-in-c.html

http://www.indiabix.com/c-programming/pointers/

Directions

Step 1: Identify solution without any assistance (compiler, textbook, Google, etc).

Step 2: Research (textbook, Google), work (compiler), and implement the solution.

Step 3: Compare your expected result and actual result.

Resolve the difference by collaborating and corroborating with your classmates. If still not resolved, check with me.

Due to compilers using different standards and some not conforming to standards, it is possible for the output to be different or
incorrect. It is important that you identify the correct version and use that as reference for the exam preparation.

Some questions might refer to topics we have not covered yet for the current exam. Save it for the future exam.

If the code generates a compilation error, then identify the reason for the error and fix it.
Questions Lesson(s) Learned
1 What is the outcome/output of the following C program, if Asq table
any? Pointing to part of function

#include<stdio.h> https://www.cquestions.com/2010/04/c-questions-
void main(void){ answers.html
int a = 320;
char *ptr;
ptr =( char *)&a;
printf("%d ",*ptr);
}
2 What is the outcome/output of the following C program, if Ptr to ptr
any? K is ptr to ptr
#include<stdio.h> Prints content of k prints address of k
void main(void){ *k prints I
int i = 3; **k prints g reference twice go to j then go to i
int *j; &=address
int **k; Content of pointer is what to look for
j=&i;
k=&j;
printf("%u %u %d ",k,*k,**k);
}
3 What is the outcome/output of the following C program, if Type cast
any?
Cant use pointer
#include<stdio.h>
void main(void){
int a = 10;
void *ptr = &a;
int *p = ptr;
printf("%u",*p);
printf("%u",*ptr);

4 What is the outcome/output of the following C program, if Pointer arithmetic “position from a to b in
any? memory”
#include<stdio.h> p-q is difference between memory location
#include<string.h>
void main(void){ *p-*q is difference between physical digits
int a = 5,b = 10,c;
int *p = &a,*q = &b;
c = p - q;
printf("%d" , c);
c = *p - *q;
printf("%d" , c);
}

5 What is the outcome/output of the following C program, if Works but shouldmt


any?

#include<stdio.h>
void main(void){
int i = 8 , j;
int *p , *q;
p = &i;
q = &j;
j = i;
printf("%d %d",*p,*q);
j= &i;
printf("%d %d",*p,*q);

}
6 What is the outcome/output of the following C program, if
any?

#include<stdio.h>
void main(void){
int i = 3;
int *j;
int **k;
j = &i;
k = &j;

printf("%d %d",j,k);
printf("%p %p",j,k);
}

7 Are the expression *ptr++ and ++*ptr are same? b


A. True
B. False
8 Which of the statements is correct about the program? c
#include<stdio.h>
void main(void)
{
int i=10;
int *j=&i;
}
A. j and i are pointers to an int
B. i is a pointer to an int and stores address of j
C. j is a pointer to an int and stores address of i
D. j is a pointer to a pointer to an int and stores address
of i

9 What will be the outcome/output of the program, if any? Purpose of pointer is to point to a variable

#include<stdio.h> C
void main(void)
{ It points to nothing
int *x;
*x=100;
printf( “ %d”,*x);
}
A. Address of x
B. 100 * the initial value in x.
C. Run time error
D. 100
10 What is the output of this C code?

555

Memory location assumption


1000 – k
2000 – p
3000 – m

void main(void)
{
int k = 5;
int *p = &k;
int **m = &p;
printf("%d %d %d\n", k, *p, **m);
}
• A. 5 5 5
• B. 5 5 3000
• C. 5 2000 3000
• D. Run time error

11 What is the output of this C code?

Memory location assumption


2000 – x
1000 – ptr

void main(void)
{
int x = 0;
int *ptr = &5;
printf("%p\n", ptr);
}
• A. 5
• B. Address of 5
• C. 2000
• D. Compile error
12 What is the output of this C code?

Memory location assumption


2000 – x
3000 – ptr Prints the address of x

int x = 0; X++ only changes the value of X not the


void main(void) address
{ b
int *ptr = &x;
printf("%p\n", ptr);
x++;
printf("%p\n ", ptr);
}
• A. 2000 2004
• B. 2000 2000
• C. 1000 1000
• D. 1000 1004

13 What is the output of this C code? Addres increments by 4 not 1 4000


4004
Memory location assumption

x:2000

ptr:3000

abc:4000

int abc = 2000;


void main(void)
{
int *ptr = &abc;
printf("%p ", &ptr);
ptr++;
printf("%p", ptr);
}
14 Comment on the following? a
const int *ptr;
• A. You cannot change the value pointed by ptr
• B. You cannot change the pointer ptr itself
• C. Both (a) and (b)
• D. You can change the pointer as well as the value
pointed by it
15 Comment on the following? b
int * const ptr;
• A. You cannot change the value pointed by ptr
• B. You cannot change the pointer ptr itself
• C. Both (a) and (b)
• D. You can change the pointer as well as the value
pointed by it
16 Comment on the following? c
const int * const ptr;
• A. You cannot change the value pointed by ptr
• B. You cannot change the pointer ptr itself
• C. Both (a) and (b)
• D. You can change the pointer as well as the value
pointed by it

17 What is the output of this C code?

int main(void)
{
int *ptr, a = 10;
ptr = &a;
*ptr += 1;
printf("%d,%d/n", *ptr, a);
}
• A. 10,10
• B. 10,11
• C. 11,10
• D. 11,11
18 What is the output of this C code?

int main(void)
{
int *ptr, a = 10;
ptr = &a;
*ptr += 1;

a++;
printf("%d,%d", *ptr, a);
}

19 What is the outcome/output of the following C program, if Lion


any?

#include<stdio.h>
void main(void){
char arr1[5]="Lion";
char arr2[]=”King”;
printf("%s",arr1);
}
20 What is the outcome/output of the following C program, if
any?

#include<stdio.h>
void main(void){
char arr1[4]="Lion";
char arr2[]=”King”;
printf("%s",arr1);
}
21 What is the outcome/output of the following C program, if
any?

#include<stdio.h>
void main(void){
char arr1[5]="The Toy Story";
char arr2[]="Lion King";

printf("%s",arr1);
}
22 What is the outcome/output of the following C program, if
any?

#include<stdio.h>

void main(void)
{
char str1[] = "Hello";
char str2[] = "Olleh";
if(str1 == str2)
printf("Equal\n");
else
printf("Unequal\n");
}
A. Equal
B. Unequal
C. Error
D. None of above
23 What is the outcome/output of the following C program, if
any?

#include<stdio.h>
void main(void)
{
char str1[] = "Hello";
char str2[] = "Help";
if(*str1 == *str2)
printf("Equal\n");
else
printf("Unequal\n");
}

24 What will be the output of the following program?

#include<stdio.h>

void main(void)
{
char str[20] = "Hello";
char *const p=str;
*p+1=’M’;
printf("%s\n", str);
}
A. Mello
B. HMello
C. HMllo
D. Compile Error : cannot change due to const pointer
E. Hello
25 Which of the following function, that finds the length of a
string, is correct?

A.
int xstrlen(char *s)
{
int length=0;
while(*s!='\0')
{ length++; s++; }
return (length);
}

B.
int xstrlen(char s)
{
int length=0;
while(*s!='\0')
length++; s++;
return (length);
}

C.
int xstrlen(char *s)
{
int length=0;
while(*s!='\0')
length++;
return (length);
}
D.
int xstrlen(char *s)
{
int length=0;
while(*s!='\0')
s++;
return (length);
26 What is the outcome/output of the following C program, if
any?
#include<stdio.h>
void main(void)
{
char str1[] = "Florida Capital";
char str2[] = "Florida State";
char *s1 = str1, *s2=str2;
while(*s1 == *s2){
printf("%c", *s1);

s1++;
s2++;
}
printf("\n");
}
27 What will be the output of following program?

#include<stdio.h>
#include<string.h>
int main(void){
char *ptr1 = NULL;
char *ptr2 = 0;
strcpy(ptr1," c");
strcpy(ptr2,"questions");
printf("\n%s %s",ptr1,ptr2);
return 0;
}

(A) c questions

(B) c (null)

(C) (null) (null)

(D) Compilation error

(E) Run time error


28 What is the outcome/output of the following C program, if
any?
#include<stdio.h>

void main(void)
{
char str1[] = "Hello";
char str2[10];
char *t, *s;
s = str1;
t = str2;
while(*t=*s)
*t++ = *s++;
printf("%s\n", str2);
}
29 What is the outcome/output of the following C program, if
any?
#include<stdio.h>

void main(void)
{
char str1[] = "Hello";
char str2[10]=”Star Wars”;
char *t, *s;
s = str1;
t = str2;
while(*t=*s)
*t++ = *s++;
printf("%s\n", str2);
}
30 What will be the output of the following program?
#include<stdio.h>

int main(void)
{
char a[] = "EGN";
char *p = "3211";
a = "3211";
p = "EGN";
printf("%s %s\n", a, p);
return 0;
}

a. Compile error. Cannot reassign string to existing


array and pointer
b. Compile error. Cannot reassign string to existing
array
c. Compile error. Cannot reassign string to existing
pointer
d. EGN 3211
e. 3211 EGN

31
What will be the output of the following program, if
any?

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

       int arr[]={2, 3, 4};


       int *ptr=arr;

       int i;

ptr++;

       for (i=0;i<3;i++)
      { 
          (*ptr)++;
       }

       for (i=0;i<3;i++)
      { 
          printf("%u ", arr[i]);
       }

32 Given
#include<stdio.h>

void main(void)
{
int a[3][4];
fun(a);
}
Which of the following is the correct way to define the
function fun() for the above program?

A.
void fun(int p[][4])
{
}
B.
void fun(int *p[4])
{
}
C.
void fun(int *p[][4])
{
}
D.
void fun(int *p[3][4])
{
}
33 Intentionally left blank

You might also like