Pointer

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

Pointer is special kind of variable which is used to store __________ of the variable.

a) b) c) d) Variable Name Data Type Value Address

Pointer variable is declared using preceding _________ sign. a) b) c) d) * & % ^

Address stored in the pointer variable is of type __________. a) Floating b) Character c) Integer d) Array Consider the 32 bit compiler. We need to store address of integer variable to integer pointer. What will be the size of integer pointer ? a) 2 Bytes b) 10 Bytes c) 4 Bytes d) 6 Bytes In order to fetch the address of the variable we write preceding _________ sign before variable name. a) b) c) d) Ampersand Percent Comma Asteriks

Code: int z,x=5,y=-10,a=4,b=2; z = x++ - --y * b / a;

What number will z in the sample code above contain? Choice 1 5 Choice 2 6 Choice 3 10 [Ans] Corrected by buddy by running the program Choice 4 11 Choice 5 12 With every use of a memory allocation function, what function should be used to release allocated memory which is no longer needed? Choice 1 unalloc() Choice 2 dropmem() Choice 3 dealloc() Choice 4 release() Choice 5 free() [Ans]
Code: void *ptr; myStruct myArray[10]; ptr = myArray;

Which of the following is the correct way to increment the variable "ptr"? Choice 1 ptr = ptr + sizeof(myStruct); [Ans] Choice 2 ++(int*)ptr; Choice 3 ptr = ptr + sizeof(myArray); Choice 4 increment(ptr); Choice 5 ptr = ptr + sizeof(ptr); What function will read a specified number of elements from a file? Choice 1 fileread()

Choice 2 getline() Choice 3 readfile() Choice 4 fread() Choice 5 gets() "My salary was increased by 15%!" Select the statement which will EXACTLY reproduce the line of text above. Choice 1 printf("\"My salary was increased by 15/%\!\"\n"); Choice 2 printf("My salary was increased by 15%!\n"); Choice 3 printf("My salary was increased by 15'%'!\n"); Choice 4 printf("\"My salary was increased by 15%%!\"\n");[Ans] Choice 5 printf("\"My salary was increased by 15'%'!\"\n"); What is a difference between a declaration and a definition of a variable? Choice 1 Both can occur multiple times, but a declaration must occur first. Choice 2 There is no difference between them. Choice 3 A definition occurs once, but a declaration may occur many times. Choice 4 A declaration occurs once, but a definition may occur many times. [Ans] Choice 5 Both can occur multiple times, but a definition must occur first. int testarray[3][2][2] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; What value does testarray[2][1][0] in the sample code above contain? Choice 1 3 Choice 2 5 Choice 3 7 Choice 4 9 Choice 5 11[Ans]
Code: int a=10,b; b=a++ + ++a;

printf("%d,%d,%d,%d",b,a++,a,++a);

what will be the output when following code is executed Choice 1 12,10,11,13 Choice 2 22,10,11,13 Choice 3 22,11,11,11 Choice 4 12,11,11,11 Choice 5 22,13,13,13[Ans]
Code: int x[] = { 1, 4, 8, 5, 1, 4 }; int *ptr, y; ptr = x + 4; y = ptr - x;

What does y in the sample code above equal? Choice 1 -3 Choice 2 0 Choice 3 4[Ans] Choice 4 4 + sizeof( int ) Choice 5 4 * sizeof( int)

11 ^ 5 What does the operation shown above produce? Choice 1 1 Choice 2 6 Choice 3 8 Choice 4 14 [Ans] Choice 5 15 #define MAX_NUM 15 Referring to the sample above, what is MAX_NUM? Choice 1

MAX_NUM is an integer variable. Choice 2 MAX_NUM is a linker constant. Choice 3 MAX_NUM is a precompiler constant. Choice 4 MAX_NUM is a preprocessor macro. [Ans] Choice 5 MAX_NUM is an integer constant What is a proper method of opening a file for writing as binary file? Choice 1 FILE *f = fwrite( "test.bin", "b" ); Choice 2 FILE *f = fopenb( "test.bin", "w" ); Choice 3 FILE *f = fopen( "test.bin", "wb" ); Choice 4 FILE *f = fwriteb( "test.bin" ); Choice 5 FILE *f = fopen( "test.bin", "bw" ); Which one of the following functions is the correct choice for moving blocks of binary data that are of arbitrary size and position in memory? Choice 1 memcpy() Choice 2 memset() Choice 3 strncpy() Choice 4 strcpy() Choice 5 memmove()[Ans] int x = 2 * 3 + 4 * 5; What value will x contain in the sample code above? Choice 1 22 Choice 2 26[Ans] Choice 3 46 Choice 4 50 Choice 5 70 int var1; If a variable has been declared with file scope, as above, can it safely be accessed globally from

another file? Choice 1 Yes; it can be referenced through the register specifier. Choice 2 No; it would have to have been initially declared as a static variable. Choice 3 No; it would need to have been initially declared using the global keyword.[Ans] Choice 4 Yes; it can be referenced through the publish specifier. Choice 5 Yes; it can be referenced through the extern specifier.
Code: int x = 0; for (x=1; x<4; x++); printf("x=%d\n", x);

What will be printed when the sample code above is executed? Choice 1 x=0 Choice 2 x=1 Choice 3 x=3 Choice 4 x=4[Ans] Choice 5 x=5
Code: char *ptr; char myString[] = "abcdefg"; ptr = myString; ptr += 5;

What string does ptr point to in the sample code above? Choice 1 fg [Ans]/*because string*/ Choice 2 efg Choice 3 defg Choice 4 cdefg Choice 5 None of the above Which one of the following will declare a pointer to an integer at address 0x200 in memory? Choice 1

int *x; *x = 0x200;[Ans] Choice 2 int *x = &0x200; Choice 3 int *x = *0x200; Choice 4 int *x = 0x200; Choice 5 int *x( &0x200 );

char ** array [12][12][12]; Consider array, defined above. Which one of the following definitions and initializations of p is valid? Choice 1 char ** (* p) [12][12] = array; [Ans] Choice 2 char ***** p = array; Choice 3 char * (* p) [12][12][12] = array; Choice 4 const char ** p [12][12][12] = array; Choice 5 char (** p) [12][12] = array;

Which one of the following is a true statement about pointers? Choice 1 Pointer arithmetic is permitted on pointers of any type. Choice 2 A pointer of type void * can be used to directly examine or modify an object of any type. Choice 3 Standard C mandates a minimum of four levels of indirection accessible through a pointer. Choice 4 A C program knows the types of its pointers and indirectly referenced data items at runtime. Choice 5 Pointers may be used to simulate call-by-reference

char buf [] = "Hello world!"; char * buf = "Hello world!"; In terms of code generation, how do the two definitions of buf, both presented above, differ? Choice 1 The first definition certainly allows the contents of buf to be safely modified at runtime; the second definition does not.

Choice 2 The first definition is not suitable for usage as an argument to a function call; the second definition is. Choice 3 The first definition is not legal because it does not indicate the size of the array to be allocated; the second definition is legal. Choice 4 They do not differ -- they are functionally equivalent. [Ans] Choice 5 The first definition does not allocate enough space for a terminating NUL-character, nor does it append one; the second definition does.

Code: char ptr1[] = "Hello World"; char *ptr2 = malloc( 5 ); ptr2 = ptr1;

What is wrong with the above code (assuming the call to malloc does not fail)? Choice 1 There will be a memory overwrite. Choice 2 There will be a memory leak. Choice 3 There will be a segmentation fault. Choice 4 Not enough space is allocated by the malloc. Choice 5 It will not compile.

What does the "auto" specifier do? Choice 1 It automatically initializes a variable to 0;. Choice 2 It indicates that a variable's memory will automatically be preserved.[Ans] Choice 3 It automatically increments the variable when used. Choice 4 It automatically initializes a variable to NULL. Choice 5 It indicates that a variable's memory space is allocated upon entry into the block.

Code: int y[4] = {6, 7, 8, 9}; int *ptr = y + 2; printf("%d\n", ptr[ 1 ] );

/*ptr+1 == ptr[1]*/

What is printed when the sample code above is executed? Choice 1 6 Choice 2 7 Choice 3 8 Choice 4 9[Ans] Choice 5 The code will not compile.

char txt [20] = "Hello world!\0"; How many bytes are allocated by the definition above? Choice 1 11 bytes Choice 2 12 bytes Choice 3 13 bytes Choice 4 20 bytes[Ans] Choice 5 21 bytes

Which one of the following is a true statement about pointers? Choice 1 They are always 32-bit values. Choice 2 For efficiency, pointer values are always stored in machine registers. Choice 3 With the exception of generic pointers, similarly typed pointers may be subtracted from each other. Choice 4 A pointer to one type may not be cast to a pointer to any other type.

Choice 5 With the exception of generic pointers, similarly typed pointers may be added to each other.

Which one of the following statements allocates enough space to hold an array of 10 integers that are initialized to 0? Choice 1 int *ptr = (int *) malloc(10, sizeof(int)); Choice 2 int *ptr = (int *) calloc(10, sizeof(int)); Choice 3 int *ptr = (int *) malloc(10*sizeof(int)); [Ans] Choice 4 int *ptr = (int *) alloc(10*sizeof(int)); Choice 5 int *ptr = (int *) calloc(10*sizeof(int));

What are two predefined FILE pointers in C? Choice 1 stdout and stderr Choice 2 console and error Choice 3 stdout and stdio Choice 4 stdio and stderr Choice 5 errout and conout

Code: long factorial (long x) { ???? return x * factorial(x - 1); }

With what do you replace the ???? to make the function shown above return the correct answer? Choice 1 if (x == 0) return 0; Choice 2

return 1; Choice 3 if (x >= 2) return 2; Choice 4 if (x == 0) return 1; Choice 5 if (x <= 1) return 1; [Ans]{more probable}

How is a variable accessed from another file? Choice 1 The global variable is referenced via the extern specifier.[Ans] Choice 2 The global variable is referenced via the auto specifier. Choice 3 The global variable is referenced via the global specifier. Choice 4 The global variable is referenced via the pointer specifier. Choice 5 The global variable is referenced via the ext specifier.

When applied to a variable, what does the unary "&" operator yield? Choice 1 The variable's value Choice 2 The variable's binary form Choice 3 The variable's address [Ans] Choice 4 The variable's format Choice 5 The variable's right value

Which one of the following is NOT a valid identifier? Choice 1 __ident Choice 2 auto [Ans] Choice 3 bigNumber Choice 4

g42277 Choice 5 peaceful_in_space


Code: FILE *f = fopen( fileName, "r" ); readData( f ); if( ???? ) { puts( "End of file was reached" ); }

Which one of the following can replace the ???? in the code above to determine if the end of a file has been reached? Choice 1 f == EOF[Ans] Choice 2 feof( f ) Choice 3 eof( f ) Choice 4 f == NULL Choice 5 !f

Global variables that are declared static are ____________. Which one of the following correctly completes the sentence above? Choice 1 Deprecated by Standard C Choice 2 Internal to the current translation unit Choice 3 Visible to all translation units Choice 4 Read-only subsequent to initialization Choice 5 Allocated on the heap[Ans]

Code: double read_double (FILE * fp) { double d; assert(fp != NULL); fscanf(fp, " %lf", d); return d; }

The code above contains a common error. Which one of the following describes it? Choice 1 fscanf() will fail to match floating-point numbers not preceded by whitespace. Choice 2 The format specifier %lf indicates that the corresponding argument should be long double rather than double. Choice 3 The call to fscanf() requires a pointer as its last argument. Choice 4 The format specifier %lf is recognized by fprintf() but not by fscanf(). Choice 5 d must be initialized prior to usage.

Which one of the following is NOT a valid C identifier? Choice 1 ___S Choice 2 1___ [Ans] Choice 3 ___1 Choice 4 ___ Choice 5 S___

Which one of the following is valid for opening a read-only ASCII file? Choice 1 fileOpen (filenm, "r"); Choice 2 fileOpen (filenm, "ra"); Choice 3 fileOpen (filenm, "read"); Choice 4

fopen (filenm, "read"); Choice 5 fopen (filenm, "r");[Ans]

f = fopen( filename, "r" ); Referring to the code above, what is the proper definition for the variable f? Choice 1 FILE f; Choice 2 FILE *f;[Ans] Choice 3 int f; Choice 4 struct FILE f; Choice 5 char *f;

If there is a need to see output as soon as possible, what function will force the output from the buffer into the output stream? Choice 1 flush() Choice 2 output() Choice 3 fflush() Choice 4 dump() Choice 5 write()

Code: char *buffer = "0123456789"; char *ptr = buffer; ptr += 5; printf( "%s\n", ptr ); printf( "%s\n", buffer );

What will be printed when the sample code above is executed? Choice 1 0123456789

56789 Choice 2 5123456789 5123456789 Choice 3 56789 56789 Choice 4 0123456789 0123456789 Choice 5 56789 0123456789 [Ans]

Code: int x[] = {1, 2, 3, 4, 5}; int u; int *ptr = x; ???? for( u = 0; u < 5; u++ ) { printf("%d-", x[u]); }

printf( "\n" ); Which one of the following statements could replace the ???? in the code above to cause the string 1-2-3-10-5- to be printed when the code is executed? Choice 1 *ptr + 3 = 10; Choice 2 *ptr[ 3 ] = 10; Choice 3 *(ptr + 3) = 10;[Ans] Choice 4 (*ptr)[ 3 ] = 10; Choice 5 *(ptr[ 3 ]) = 10;

When applied to a variable, what does the unary "&" operator yield? Choice 1 The variable's address [Ans] Choice 2

The variable's right value Choice 3 The variable's binary form Choice 4 The variable's value Choice 5 The variable's format

Which one of the following calls will open the file test.txt for reading by fgetc? Choice 1 fopen( "test.txt", "r" ); Choice 2 read( "test.txt" ) Choice 3 fileopen( "test.txt", "r" ); Choice 4 fread( "test.txt" ) Choice 5 freopen( "test.txt" )

c = getchar(); What is the proper declaration for the variable c in the code above? Choice 1 char *c; Choice 2 unsigned int c; Choice 3 int c; Choice 4 unsigned char c; Choice 5 char c;[Ans]

1. Character Set of C language contains ? A. B. C. D. A. Alphabets Digits Special Symbols All of these 2. A variable name can have ? Any special symbol

B. C. D. A. B. C. D. A. B. C. D. A. B. C. D. A. B. C. D. A. B. C. D. A. B. C. D. A. B. C. D. A. B. C. D.

blank space comma ( , ) underscore 3. In C language , one of the following is not a valid data type long float double char 4. The format string %lf is used for ? float double unsigned int long double 5. A variable of type unsigned int can have a value in the range of ? -32768 to +32767 0 to 32767 0 to 65535 -32767 to +32767 6. Which data type is not a primary data type int array float char 7. Which of the format string is not valid ? %ld %lf %lu %lc 8. Which is the valid string data 'A' A "A" None of these 9. How much memory is required to store a value of type double ? 4 bytes 6 bytes 8 bytes 10 bytes 10. The modifier which is used to declare a variable as constant short signed unsigned const

What will be the output of the following C code when executed assume that all the header files are declared, char *str = "hello, world"; char *str1 = "hello, world"; if (strcmp(str, str1)) printf("equal"); else printf("unequal"); *

equal unequal Compilation error Depends on the compiler

What will be the output of the following C-code char *str = "hello, world"; char str1[9]; strncpy(str1, str, 9); printf("%s %d", str1, strlen(str1)); *

Compilation error Undefined behaviour hello, wor 9 hello, world 11

Strcat function adds null character *


Only if there is space Always Depends on the standard Depends on the compiler

What is the output of the following code. void main() { char c=125; c=c+10; printf("%d",c); } *

135 +INF -121 -8

The return-type used in String operations are. *


void only void and (char *) only void and int only void, int and (char *) only

String operation such as strcat(s, t), strcmp(s, t), strcpy(s, t) and strlen(s) heavily rely upon. *

Presence of NULL character Presence of new-line character Presence of any escape sequence All of the above

void main() { char *str="Hello world"; printf("%d",printf("%s",str)); } *


11Hello world 10Hello world Hello world10 Hello world11

Which of the following function sets first n characters of a string to a given character? *

strset() strinit() strnset() strcset()

Which of the following function compares 2 strings with case-insensitively? *


strcmp(s, t) strcmpcase(s, t) strcasecmp(s, t) strchr(s, t)

How will you print \n on the screen? *


printf("\n"); printf('\n'); echo "\\n"; printf("\\n");

1. What will be the output of the following program : void main() { char str[]="C For Swimmers"; printf("\n%.5s",str); } *

C For Swimmers C For For S Swimmers

2. What will be the output of the following program : void main() { char a[80],b[80],c[80]; scanf("%1s %5s %3s",a,b,c); printf("%s %s %s",a,b,c); } [NOTE : THE USER INPUT IS: CMeansSea Ocean Vast] *

COV C Means Sea C Ocean Vas None of these

3. What will be the output of the following program : void main() { char line[80]; scanf("%[^,]s",line); printf("\n%s",line); } [NOTE : THE USER INPUT IS:Dear Friends, What is the output?] *

Compile Time Error Dear Friends What is the output? None of these

4. Which of the following function sets first n characters of a string to a given character? *

strinit( ) strnset( ) strset( ) strcset( )

5. The library function used to find the last occurrence of a character in a string is: *

strnstr( ) laststr( ) strrchr( ) strstr( )

6. What will be output if you will compile and execute the following C code? main() { char *str=NULL; strcpy(str,"Cquestionbank"); printf("%s",str); } *

Cquestionbank Cquestionbank/0 (null) Nothing will be printed 7. What would be the output of this program? main() { char a[]=India; char *p=BITS; a=BITS; p=India; printf(%s %s,a,p); } *

India BITS BITS India Compile Time Error Run Time Error

8. Which of the following statements is incorrect about the below declarations? char *p = "Sanjay"; char a[] = "Sanjay"; *

There is no difference in the declarations and both serve the same purpose. p is a non-const pointer pointing to a non-const string, whereas a is a const pointer pointing to a non-const pointer. The pointer p can be modified to point to another string, whereas the individual characters within array a can be changed. In both cases the '\0' will be added at the end of the string "Sanjay". 9. What will print when this code is executed? main() { char *x=HELLO; x+=3; printf(%s,x); } *

HELLO ELLO LLO LO 10. What will be the output of the program ? main() { char p[]=%d\n; p[1]=c; printf(p,65); } *

A a

C 65 The correct syntax to send a 3-dimensional array as a parameter to a function is *

void func(a); void func(&a); void func(*a); void func(**a);

What is the output of the following C code? #include <stdio.h> int main() { int ary[2][3]; ary[ ][ ] = {{1, 2, 3}, {4, 5, 6}}; printf("%d\n", ary[1][0]); } *

Compile time error 4 1 2

What is the output of this C code? #include <stdio.h> void main() { int a[3] = {1, 2, 3}; int *p = a; printf("%p\t%p", p, a); } *

Same address is printed. Different address is printed. Compile time error Nothing

Predict the Output of the Following Program ? #include<stdio.h> void main() { char str[ ]="lets play cricket"; printf("%s",str); } *

Error : Declaration not Allowed lets play cricket Array Size not Specified None Of these

What is the output of this C code? #include <stdio.h> void main() { char *s= "hello"; char *p = s; printf("%c\t%c", p[0], s[1]); } *

Run time error hh

he hl

What will happen if in a C program you assign a value to an array element whose subscript exceeds the size of array? *

The element will be set to 0. The compiler would report an error. The program may crash if some important data gets overwritten. The array size would appropriately grow.

What does the following declaration mean? int (*ptr)[10]; *


ptr is array of pointers to 10 integers ptr is a pointer to an array of 10 integers ptr is an array of 10 integers ptr is an pointer to array

What is the maximun number of dimensions an array in C may have? *


2 8 20 Theoritically no limit. The only practical limits are memory size and compilers

Which of the following is correct way to define the function fun() in the below program? *

void fun(int p[ ][4]) { } void fun(int *p[4]) { } void fun(int *p[][4]) { } void fun(int *p[3][4]) { }

What is the output of the code given below? #include <stdio.h> int main() { int ary[4] = {1, 2, 3, 4}; int *p = ary + 3; printf("%d\n", p[-2]); } *

1 2 compilation error

garbage value will be printed 1. To print an address string, which specifier should be used? *

%p %d %s Address cannot be printed

2. NULL is a special pointer constant defined under which header file? *


conio.h stdio.h math.h None of these

3. Predict the output of the given code snippet (Assume no errors are present): int a = 30, b=6; int *ptr1 = &a, *ptr2 = &b; int y = 6*-*ptr1/*ptr2+30; printf(%d,&y); *

0 -5 150 8

4. Which of the following is/are FALSE? *


A pointer holds the address of the very first byte of memory location where it is pointing to Pointers can be multiplied An integer may be added to or subtracted from a pointer All are false

5. Consider the code fragment: int num[] = {24,34,12}; int i = 0; Then, to print the first number of the array, which of the following statements cannot be used? *

printf(%d,*(num+i)); printf(%d,*(i+num)); printf(%d,i[num]); All can be used

6. What will be the output of this code fragment : int student[3][2]= { {1234,56}, {1212,33}, {1434,80}}; int i=0, j=1; printf(%d,*(*(student+i)+j)); *

56 33 1234 80

7. Consider a pointer: float *f; Suppose that initially, f points to a memory location with address 2345000. Then, after the operation f=f+2, f = ? (Size of float = 4) *

2345002 2345008 2345010 2345012

8. Which of the following operations is not allowed? int *p1,*p2,*p3; int x = 2, y = 3; p1 = &x; p2 = &y; *

p3 =p1+p2; p1++ p2-p1+=2

9. Consider the two pointers: int n=44; int *ptr=&n; int *const cptr=&n; Pick the incorrect statement: *

++(*ptr) is a valid operation ++(*cptr) does not produce an error ++cptr does not produce an error All are correct

10. int x[3][5] = { {1,2,3,4,5} , {6,7,8,9,10}, {11,12,13,14,15} }; Pick the incorrect statement: *

printf(%d,*(*(x+2)+1)) prints 12 printf(%d,*(*(x+1))) prints 6 printf(%d,*(*(x)+2)+1) prints 5 printf("%d",*(*(x+1)+3) prints 9

You might also like