Sunday, 29 August 2010

c pointer objective questions with answers


1) What will be output of following program?
void main()
{
int a=320;
char *ptr;
ptr=(char *)&a;
printf("%d ",*ptr);
getch();
}
(a) 2
(b) 320
(c) 64
(d)Compiler error
(e)None of above
 (2) What will be output of following program?
#include"stdio.h"
#include"conio.h"
void main()
{
void (*p)();
int (*q)();
int (*r)();
p=clrscr;
q=getch;
r=puts;
(*p)();
(*r)("cquestionbank.blogspot.com");
(*q)();
}
(a) NULL
(b) cquestionbank.blogspot.com
(c) c
(d) Compiler error
(e) None of above
(3) What will be output of following program?

void main()
{
int i=3;
int *j;
int **k;
j=&i;
k=&j;
printf(“%u %u %d ”,k,*k,**k);
}
(a) Address, Address, 3
(b)Address, 3, 3
(c) 3, 3, 3
(d) Compiler error
(e) None of above
 (4) What will be output of following program?
void main()
{
char far *p=(char far *)0x55550005;
char far *q=(char far *)0x53332225;
*p=80;
(*p)++;
printf("%d",*q);
getch();
}
(a) 80
(b) 81
(c) 82
(d) Compiler error
(e) None of above
 (5) What will be output of following program?

#include"stdio.h"
#include"string.h"
void main()
{
char *ptr1=NULL;
char *ptr2=0;
strcpy(ptr1," c");
strcpy(ptr2,"questions");
printf("\n%s %s",ptr1,ptr2);
getch();
}
(a) c questions
(b) c (null)
(c) (null) (null)
(d) Compiler error
(e) None of above

 (6) What will be output of following program?
void main()
{
int huge *a=(int huge *)0x59990005;
int huge *b=(int huge *)0x59980015;
if(a==b)
printf("power of pointer");
else
printf("power of c");
getch();
}
(a) power of pointer
(b) power of c
(c) power of cpower of c
(d) Compiler error
(e) None of above

 (7) What will be output of following program?

#include"stdio.h"
#include"string.h"
void main()
{
register a=25;
int far *p;
p=&a;
printf("%d ",*p);
getch();
}
(a) 25
(b) 4
(c) Address
(d) Compiler error
(e) None of above

 (8) What will be output of following program?
#include"stdio.h"
#include"string.h"
void main()
{
char far *p,*q;
printf("%d %d",sizeof(p),sizeof(q));
getch();
}
(a) 2 2
(b) 4 4
(c) 4 2
(d) 2 4
(e) None of above
 (9) What will be output of following program?
void main()
{
int a=10;
void *p=&a;
int *ptr=p;
printf("%u",*ptr);
getch();
}
(a) 10
(b) Address
(c) 2
(d) Compiler error
(e) None of above

 (10) What will be output of following program?
#include"stdio.h"
#include"string.h"
void main()
{
int register a;
scanf("%d",&a);
printf("%d",a);
getch();
}
//if a=25

(a) 25
(b) Address
(c) 0
(d) Compiler error
(e) None of above
 (11) What will be output of following program?
void main()
{
char arr[10];
arr="world";
printf("%s",arr);
getch();
}
(a) world
(b) w
(c) Null
(d) Compiler error
(e) None of above
 (12) What will be output of following program?

#include"stdio.h"
#include"string.h"
void main()
{
int a,b,c,d;
char *p=0;
int *q=0;
float *r=0;
double *s=0;
a=(int)(p+1);
b=(int)(q+1);
c=(int)(r+1);
d=(int)(s+1);
printf("%d %d %d %d",a,b,c,d);
}
(a)2 2 2 2
(b)1 2 4 8
(c)1 2 2 4
(d) Compiler error
(e) None of above
 (13) What will be output of following program?

#include"stdio.h"
#include"string.h"
void main()
{
int a=5,b=10,c;
int *p=&a,*q=&b;
c=p-q;
printf("%d",c);
getch();
}
(a) 1
(b) 5
(c) -5
(d) Compiler error
(e) None of above

(14) What will be output of following program?

unsigned long int (* avg())[3]
{
static unsigned long int arr[3]={1,2,3};
return &arr;
}
void main()
{
unsigned long int (*ptr)[3];
ptr=avg();
printf("%d",*(*ptr+2));
getch();
}
(a) 1
(b) 2
(c) 3
(d) Compiler error
(e) None of above
 (15) What will be output of following program?

void main()
{
int * p,b;
b=sizeof(p);
printf(“%d”,b);
}
(a) 2
(b) 4
(c) 8
(d) Compiler error
(e) None of above

(16) What will be output of following program?

void main()
{
int i=5,j;
int *p,*q;
p=&i;
q=&j;
j=5;
printf("value of i : %d value of j : %d",*p,*q);
getch();
}
(a) 5 5
(b) Address Address
(c) 5 Address
(d) Compiler error
(e) None of above

(17) What will be output of following program?

{
int i=5;
int *p;
p=&i;
printf(" %u %u",*&p,&*p);
getch();
}
(a) 5 Address
(b) Address Address
(c) Address 5
(d) Compiler error
(e) None of above

(18) What will be output of following program?
 
void main()
{
int i=100;
printf("value of i : %d addresss of i : %u",i,&i);
i++;
printf("\nvalue of i : %d addresss of i : %u",i,&i);
getch();
}
(a) value of i : 100 addresss of i : Address
value of i : 101 addresss of i : Address
(b) value of i : 100 addresss of i : Address
value of i : 100 addresss of i : Address
(c) value of i : 101 addresss of i : Address
value of i : 101 addresss of i : Address
(d) Compiler error
(e) None of above
 (19) What will be output of following program?
 
void main()
{
char far *p=(char far *)0x55550005;
char far *q=(char far *)0x53332225;
*p=25;
(*p)++;
printf("%d",*q);
getch();
}
(a) 25
(b) Address
(c) Garbage
(d) Compiler error
(e) None of above


(20) What will be output of following program?
 
void main()
{
int i=3;
int *j;
int **k;
j=&i;
k=&j;
printf(“%u %u %u”,i,j,k);
}
(a) 3 Address 3
(b) 3 Address Address
(c) 3 3 3
(d) Compiler error
(e) None of above










Answer:
(1) c
(2) b
(3) a
(4) b
(5) c
(6) a
(7) d
(8) c
(9) a
(10) d
(11) d
(12) b
(13) a
(14) c
(15) e
(16) a
(17) b
(18) a
(19) e
(20) b

Explanation
(1)
As we know int is two byte data byte while char is one byte data byte. char pointer can keep the address one byte at time.
Binary value of 320 is 00000001 01000000 (In 16 bit)
Memory representation of int a=320 is:





So ptr is pointing only first 8 bit which color is green and
Decimal value is 64.
(2)
p is pointer to function whose parameter is void and return type
is also void. r and q is pointer to function whose parameter is
void and return type is int . So they can hold the address of
such function.
(3)
Memory representation


Here 6024, 8085, 9091 is any arbitrary address, it may be different.
Value of k is content of k in memory which is 8085
Value of *k means content of memory location which address k keeps.
k keeps address 8085 .
Content of at memory location 8085 is 6024
In the same way **k will equal to 3.

Short cut way to calculate:
Rule: * and & always cancel to each other
i.e. *&a=a
So *k=*(&j) since k=&j
*&j=j =6024
And
**k=**(&j)=*(*&j)=*j=*(&i)=*&i=i=3
(4)
Far address of p and q are representing same physical address .
Physical address of 0x55550005= (0x5555)*(0x10) + (0x0005) = 0x55555
Physical address of 0x53332225= (0x5333*0x10) + (0x2225) =0x55555
*p =80, means content at memory location 0x55555 is assigning value 25
(*p)++ means increase the content by one at memory location 0x5555 so now

content at memory location 0x55555 is 81
*q also means content at memory location 0x55555 which is 26
(5)
We cannot assign any string constant in null pointer by strcpy function.
(6)
Here we are performing relational operation between two huge addresses. So

first both a and b will normalize.
a= (0x5999)* (0x10) + (0x0005) =0x9990+0x0005=0x9995
b= (0x5998)* (0x10) + (0x0015) =0x9980+0x0015=0x9995
Here both huge addresses are representing same physical address. So a==b

is true.
(7)
Register data type stores in CPU. So it has not any memory
address. Hence we cannot write &a.
(8)
p is far pointer which size is 4 byte.
By default q is near pointer which size is 2 byte.
(9)
void pointer can hold address of any data type without type casting. Any

pointer can hold void pointer without type casting.
(10)
Register data type stores in CPU. So it has not any memory address. Hence

we cannot write &a.

(11) Compiler error Lvalue required
Array name is constant pointer and we cannot assign any
value in constant data type after declaration.

(12)
Address=next address
Since initial address of all data type is zero. So its
next address will be size of data type.

(13)
Difference of two same type of pointer is always one.

(15)
Output: 2 or 4
since in this question it has not written p is which type
of pointer. So its output will depend upon which memory
model has selected. Default memory model is small.


(17)
Since * and & always cancel to each other.
i.e. *&a=a
so *&p=p which store address of integer i
&*p=&*(&i) //since p=&i
=&(*&i)
=&i
So second output is also address of i

(18)
Within the scope of any variable, value of variable
may change but its address will never change in any
modification of variable.

(19)
Far address of p and q are representing same physical
address. Physical address of
0x55550005= 0x5555*ox10+ox0005= 0x55555
Physical address of
0x53332225=0x5333*0x10+ox2225=0x55555
*p =25, means content at memory location 0x55555 is
assigning value 25
(*p)++ means increase the content by one at memory
location 0x5555 so now content at memory location 0x55555
is 26
*q also means content at memory location 0x55555 which is
26


(20)

Here 6024, 8085, 9091 is any arbitrary address, it may be different.


Read more: http://cmagical.blogspot.com/2009/11/pointer-objective-questions-with.html#ixzz0y172akrn
Under Creative Commons License: Attribution Non-Commercial No Derivatives

Tricky questions on C

1)Write a C program to find a peculiar two digit number which is three times the sum of its digits.

2) Bacteria are known to multiply very rapidly. If a certain container contains just one bacterium on the first day and there are twice as many on the next day. In this manner the number of bacteria in the container doubles itself everyday. Assuming that the container would be full on the 10th day with 13,312 bacteria, find the number of bacteria that was initially in the container on the first day.

3) Calculate the factorial of a number recursively. From that calculate the value of COS(X) = 1 - X2/2! + X4 /4! - X6/ 6! +…….

4) A number of “Cats” got together and decided to kill between them 999919 mice. Every cat killed equal number of “mice”. Write a program to find number of cats.

5) Consider the following number 45*45=2025; 20+25=45.Write a program to generate number between 32 and 99 that satisfies the above property.

6) Rita has a money pouch containing Rs.700. There are equal number of 25 paise coins, 50 paise and one rupee coins. Write a C program to find how many of each are there?

7) Calculate the factorial of a number recursively and from that calculate the value of ex=1+ (X1/1!) + (X2 /2!) + (X3/3!) + ……….

8) There are some goats and ducks in a farm. There are 60 eyes and 86 foot in total. Write a program to find number of goats and ducks in the farm.

9) Write a C program to find a three digit number which is greater than the aggregate of its third, tenth and the twelfth parts by 58.

10) Write a C program to find a two digit number, the second digit of which is smaller than its first digit by 4, and if the number was divided by the digit’s sum, the quotient would be 7.

Read more: http://cmagical.blogspot.com/2009/11/technical-question-31-of-32-print.html#ixzz0y16ofSCW

C Programming on Unix ( Creating menu driven environment in C in Unix)


Creating menu driven environment in C in Unix

#include
#include
#define ENTER 10
#define ESCAPE 27
void init_curses()
{
initscr();
start_color();
init_pair(1,COLOR_WHITE,COLOR_BLUE);
init_pair(2,COLOR_RED,COLOR_GREEN);
init_pair(3,COLOR_RED,COLOR_WHITE);
init_pair(4,COLOR_BLUE,COLOR_YELLOW);
init_pair(5,COLOR_BLUE,COLOR_CYAN);
curs_set(0);
noecho();
keypad(stdscr,TRUE);
}
void draw_menubar(WINDOW *menubar)
{
wbkgd(menubar,COLOR_PAIR(5));
waddstr(menubar,"File");
wattron(menubar,COLOR_PAIR(3));
waddstr(menubar,"(F1)");
wattroff(menubar,COLOR_PAIR(3));
wmove(menubar,0,10);
waddstr(menubar,"Edit");
wattron(menubar,COLOR_PAIR(3));
waddstr(menubar,"(F2)");
wmove(menubar,0,20);
wattroff(menubar,COLOR_PAIR(3));
waddstr(menubar,"Search");
wattron(menubar,COLOR_PAIR(3));
waddstr(menubar,"(F3)");
wmove(menubar,0,31);
wattroff(menubar,COLOR_PAIR(3));
waddstr(menubar,"Cursor ");
wattron(menubar,COLOR_PAIR(3));
waddstr(menubar,"(F4)");
wmove(menubar,0,43);
wattroff(menubar,COLOR_PAIR(3));
waddstr(menubar,"Compile");
wattron(menubar,COLOR_PAIR(3));
waddstr(menubar,"(F5)");
wattroff(menubar,COLOR_PAIR(3));
wmove(menubar,0,56);
waddstr(menubar,"Help");
wattron(menubar,COLOR_PAIR(3));
waddstr(menubar,"(F6)");
wattroff(menubar,COLOR_PAIR(3));
wmove(menubar,0,66);
waddstr(menubar,"Exit");
wattron(menubar,COLOR_PAIR(3));
waddstr(menubar,"(F7)");

}
WINDOW **draw_menu(int start_col)
{
int i;
WINDOW **items;
items=(WINDOW **)malloc(9*sizeof(WINDOW *));
items[0]=newwin(6,19,1,start_col);
wbkgd(items[0],COLOR_PAIR(2));
// box(items[0],ACS_VLINE,ACS_HLINE);
//wprintw(items[1],"new");

items[1]=subwin(items[0],1,17,2,start_col+1);
items[2]=subwin(items[0],1,17,3,start_col+1);
items[3]=subwin(items[0],1,17,4,start_col+1);
items[4]=subwin(items[0],1,17,5,start_col+1);
items[5]=subwin(items[0],1,17,6,start_col+1);
items[6]=subwin(items[0],1,17,7,start_col+1);
/// items[7]=subwin(items[0],1,17,8,start_col+1);
// items[8]=subwin(items[0],1,17,9,start_col+1);
/// for (i=1;i<9;i++)

wprintw(items[1],"Load",i);
wprintw(items[2],"New",i);
wprintw(items[3],"Save");
wprintw(items[4],"Save as");
wprintw(items[5],"Printer");
/// wprintw(items[],"FILE%d",i);
/// wprintw(item[1],"File%d",i);
// wbkgd(items[1],COLOR_PAIR(1));
wrefresh(items[0]);
return items;
}
WINDOW **draw_menu2(int start_col)
{
int i;
WINDOW **items;
items=(WINDOW**) malloc(9*sizeof(WINDOW*));
items[0]=newwin(5,19,1,start_col);
wbkgd(items[0],COLOR_PAIR(2));

items[1]=subwin(items[0],1,17,2,start_col+1);
items[2]=subwin(items[0],1,17,3,start_col+1);
items[3]=subwin(items[0],1,17,4,start_col+1);
items[4]=subwin(items[0],1,17,5,start_col+1);
items[5]=subwin(items[0],1,17,6,start_col+1);
items[6]=subwin(items[0],1,17,7,start_col+1);

wprintw(items[1],"Find");
wprintw(items[2],"Find and replace");
wprintw(items[3],"Repeat last find");
wprintw(items[4],"Goto line number");
wrefresh(items[0]);
return items;
}


WINDOW **draw_menu1(int start_col)
{

int i;
WINDOW **items;
items=(WINDOW**) malloc(9*sizeof(WINDOW*));
items[0]=newwin(6,19,1,start_col);
wbkgd(items[0],COLOR_PAIR(2));

items[1]=subwin(items[0],1,17,2,start_col+1);
items[2]=subwin(items[0],1,17,3,start_col+1);
items[3]=subwin(items[0],1,17,4,start_col+1);
items[4]=subwin(items[0],1,17,5,start_col+1);
items[5]=subwin(items[0],1,17,6,start_col+1);
items[6]=subwin(items[0],1,17,7,start_col+1);

wprintw(items[1],"Undo");
wprintw(items[2],"Cut");
wprintw(items[3],"Copy");
wprintw(items[4],"Paste");
wprintw(items[5],"Delete");
wrefresh(items[0]);
return items;
}

WINDOW **draw_menu4(int start_col)
{
int i;
WINDOW **items;
items=(WINDOW**) malloc(9*sizeof(WINDOW*));
items[0]=newwin(5,19,1,start_col);
wbkgd(items[0],COLOR_PAIR(2));

items[1]=subwin(items[0],1,17,2,start_col+1);
items[2]=subwin(items[0],1,17,2,start_col+1);
items[3]=subwin(items[0],1,17,4,start_col+1);
items[4]=subwin(items[0],1,17,4,start_col+1);
// items[5]=subwin(items[0],1,17,4,start_col+1);
// items[6]=subwin(items[0],1,17,4,start_col+1);
wprintw(items[2],"C Compiler");
wprintw(items[4],"C++ Compiler");
wrefresh(items[0]);

return items;
}


WINDOW **draw_menu5(int start_col)
{
int i;
WINDOW **items;
items=(WINDOW**) malloc(9*sizeof(WINDOW*));
items[0]=newwin(4,19,1,start_col);
wbkgd(items[0],COLOR_PAIR(2));


items[1]=subwin(items[0],1,17,2,start_col+1);
items[2]=subwin(items[0],1,17,2,start_col+1);
items[3]=subwin(items[0],1,17,2,start_col+1);
items[4]=subwin(items[0],1,17,4,start_col+1);
// items[5]=subwin(items[0],1,17,4,start_col+1);
// items[6]=subwin(items[0],1,17,4,start_col+1);

wprintw(items[2],"Key words");
// wprintw(items[4]," perator");
wprintw(items[4],"Operator");
// wprintw(items[6],"Help");
wrefresh(items[0]);

return items;

}

void delete_menu(WINDOW **items,int count)
{
int i;
for (i=0;i
delwin(items[i]);
free(items);
}
int scroll_menu(WINDOW **items,int count,int menu_start_col)
{
int key;
int selected=0;
while (1) {
key=getch();
if (key==KEY_DOWN || key==KEY_UP) {
wbkgd(items[selected+1],COLOR_PAIR(2));
wnoutrefresh(items[selected+1]);
if (key==KEY_DOWN) {
selected=(selected+1) % count;
} else {
selected=(selected+count-1) % count;
}
wbkgd(items[selected+1],COLOR_PAIR(1));
wnoutrefresh(items[selected+1]);
doupdate();
} else if (key==KEY_LEFT || key==KEY_RIGHT) {
delete_menu(items,count+1);
touchwin(stdscr);
refresh();
items=draw_menu(60-menu_start_col);
return scroll_menu(items,5,60-menu_start_col);
} else if (key==ESCAPE) {
return -1;
} else if (key==ENTER) {
return selected;
}
}
}
int main()
{
int key,t;
WINDOW *menubar,*messagebar,*window;
init_curses();
bkgd(COLOR_PAIR(1));
window=subwin(stdscr,1,80,24,0);
wbkgd(window,COLOR_PAIR(4));
menubar=subwin(stdscr,1,80,0,0);
messagebar=subwin(stdscr,1,79,23,1);
draw_menubar(menubar);
move(2,1);
printw("Press F1 or F2 to open the menus. ");
printw("F9 quits.");
/// window=newwin(2,68,23,8);
/// wbkgd(window,COLOR_PAIR(4));
///t=wgetch(window);
refresh();

do {
int selected_item;
WINDOW **menu_items;
key=getch();
werase(messagebar);
wrefresh(messagebar);
if (key==KEY_F(1))
{
menu_items=draw_menu(0);
selected_item=scroll_menu(menu_items,5,0);
delete_menu(menu_items,9);

if (selected_item<0)
wprintw(messagebar,
"You haven't selected any item.");
else
wprintw(messagebar, "You have selected menu item %d.",selected_item+1);
touchwin(stdscr);
refresh();
} else if (key==KEY_F(2))
{
menu_items=draw_menu1(10);
selected_item=scroll_menu(menu_items,5,10);
delete_menu(menu_items,9);

if (selected_item<0)
wprintw(messagebar,"You haven't selected any item.");
else
wprintw(messagebar, "You have selected menu item %d.",selected_item+1);
// touchwin(stdscr);
/// window=newwin(2,68,23,12);
///wbkgd(window,COLOR_PAIR(4));
/// t=wgetch(window);
touchwin(stdscr);
refresh();
}
else if(key==KEY_F(3))
{
menu_items=draw_menu2(20);
selected_item=scroll_menu(menu_items,5,20);
delete_menu(menu_items,9);

if(selected_item<0)
wprintw(messagebar,"don't select any item");
else
wprintw(messagebar, "You have select menu item %d",selected_item+1);
touchwin(stdscr);
refresh();
}
else if(key==KEY_F(5))
{
menu_items=draw_menu4(40);
selected_item=scroll_menu(menu_items,5,40);
delete_menu(menu_items,9);

if(selected_item<0)
wprintw(messagebar, "No item is selected");
else
wprintw(messagebar, "You have selected menu %d",selected_item+1);
touchwin(stdscr);
refresh();
}
else if(key==KEY_F(6))
{
menu_items=draw_menu5(55);
selected_item=scroll_menu(menu_items,5,55);
delete_menu(menu_items,9);

if(selected_item<0)
wprintw(messagebar,
" No item is selected");
else
wprintw(messagebar, "You have selected menu %d",selected_item+1);
touchwin(stdscr);
refresh();
}

else if(key==KEY_F(7))
{
menu_items=draw_menu4(60);
selected_item=scroll_menu(menu_items,5,60);
delete_menu(menu_items,9);

if(selected_item<0)
wprintw(messagebar, "No item is selected");
else
wprintw(messagebar, "You have to select menu %d",selected_item+1);

touchwin(stdscr);
refresh();
}
} while (key!=KEY_F(9));

delwin(menubar);
delwin(messagebar);
delwin(window);
endwin();
return 0;
}


Read more: http://cmagical.blogspot.com/2010/01/c-programming-on-unix-creating-menu.html#ixzz0y16dZleY
Under Creative Commons License: Attribution Non-Commercial No Derivatives