Wednesday, 17 August 2011


C,C++ Questions
1. Base class has some virtual method and derived class has a method with the same name. If we initialize the base class pointer with derived
object, calling of that virtual method will result in which method being called? 

a. Base method
b. Derived method.

Ans. b
2. For the following C program
#define AREA(x)(3.14*x*x)
main()
{float r1=6.25,r2=2.5,a;
a=AREA(r1);
printf("\n Area of the circle is %f", a);
a=AREA(r2);
printf("\n Area of the circle is %f", a);
}

What is the output?
Ans. Area of the circle is 122.656250
        Area of the circle is 19.625000

3. What do the following statements indicate? Explain.
·         int(*p)[10]
·         int*f() à function returning pointer to int
·         int(*pf)() àpf is a pointer pointing to a function that returns an int value, pf is a function pointer
·         int*p[10]
Refer to:
-- Kernighan & Ritchie page no. 122
-- Schaum series page no. 323

4.
void main()
{
int d=5;
printf("%f",d);
}

Ans: Undefined




5.
void main()
{
int i;
for(i=1;i<4,i++)
switch(i)
case 1: printf("%d",i);break;
{
case 2:printf("%d",i);break;
case 3:printf("%d",i);break;
}
switch(i) case 4:printf("%d",i);
}

Ans: Errors

6.
void main()
{
char *s="\12345s\n";
printf("%d", sizeof(s));
}

Ans: 2

7.
void main()
{
unsigned i=1; /* unsigned char k= -1 => k=255; */
signed j=-1; /* char k= -1 => k=65535 */
/* unsigned or signed int k= -1 =>k=65535 */
if(i<j)
printf("less");
else
if(i>j)
printf("greater");
else
if(i==j)
printf("equal");
}

Ans: less




8.
void main()
{
float j;
j=1000*1000;
printf("%f",j);
}

1. 1000000
2. Overflow
3. Error
4. None

Ans: 4

9.  How do you declare an array of N pointers to functions returning
     pointers to functions returning pointers to characters?

Ans: The first part of this question can be answered in at least
        three ways:

    1. char *(*(*a[N])())();

    2. Build the declaration up incrementally, using typedefs:

        typedef char *pc;    /* pointer to char */
        typedef pc fpc();    /* function returning pointer to char */
        typedef fpc *pfpc;    /* pointer to above */
        typedef pfpc fpfpc();    /* function returning... */
        typedef fpfpc *pfpfpc;    /* pointer to... */
        pfpfpc a[N];         /* array of... */

    3. Use the cdecl program, which turns English into C and vice
    versa:

        cdecl> declare a as array of pointer to function returning
            pointer to function returning pointer to char
        char *(*(*a[])())()

    cdecl can also explain complicated declarations, help with
    casts, and indicate which set of parentheses the arguments
    go in (for complicated function definitions, like the one
    above).
    Any good book on C should explain how to read these complicated
    C declarations "inside out" to understand them ("declaration
    mimics use").
    The pointer-to-function declarations in the examples above have
    not included parameter type information. When the parameters
    have complicated types, declarations can *really* get messy.
    (Modern versions of cdecl can help here, too.)


10. A structure pointer is defined of the type time . With 3 fields min,sec hours having pointers to integers.
    Write the way to initialize the 2nd element to 10.


11. In the above question an array of pointers is declared.
    Write the statement to initialize the 3rd element of the 2 elements to 10;


12.
int f()
void main()
{
f(1);
f(1,2);
f(1,2,3);
}
f(int i,int j,int k)
{
printf("%d %d %d",i,j,k);
}

What are the number of syntax errors in the above?

Ans: one.


13.
void main()
{
int i=7;
printf("%d",i++*i++);
}

Ans: 56


14.
#define one 0
#ifdef one
printf("one is defined ");
#ifndef one
printf("one is not defined ");

Ans: "one is defined"





15.
void main()
{
int count=10,*temp,sum=0;
temp=&count;
*temp=20;
temp=&sum;
*temp=count;
printf("%d %d %d ",count,*temp,sum);
}

Ans: 20 20 20


16. There was question in c working only on unix machine with pattern matching.


14. what is alloca()

Ans : It allocates and frees memory after use/after getting out of scope

17.
main()
{
static i=3;
printf("%d",i--);
return i>0 ? main():0;
}

Ans: 321


18.
char *foo()
{
char result[100]);
strcpy(result,"anything is good");
return(result);
}
void main()
{
char *j;
j=foo()
printf("%s",j);
}

Ans: anything is good.


19.
void main()
{
char *s[]={ "dharma","hewlett-packard","siemens","ibm"};
char **p;
p=s;
printf("%s",++*p);
printf("%s",*p++);
printf("%s",++*p);
}

Ans: "harma" (p->add(dharma) && (*p)->harma)
"harma" (after printing, p->add(hewlett-packard) &&(*p)->harma)
"ewlett-packard"

20. Output of the following program is
main()
{int i=0;
for(i=0;i<20;i++)
{switch(i)
case 0:i+=5;
case 1:i+=2;
case 5:i+=5;
default i+=4;
break;}
printf("%d,",i);
}
}

a) 0,5,9,13,17
b) 5,9,13,17
c) 12,17,22
d) 16,21
e) Syntax error

Ans. (d)
21. What is the ouptut in the following program
main()
{char c=-64;
int i=-32
unsigned int u =-16;
if(c>i)
{printf("pass1,");
if(c<u)
printf("pass2");
else
printf("Fail2");
}
else
printf("Fail1);
if(i<u)
printf("pass2");
else
printf("Fail2")
}

a) Pass1,Pass2
b) Pass1,Fail2
c) Fail1,Pass2
d) Fail1,Fail2
e) None of these

Ans. (c)
22. What will the following program do?
void main()
{
int i;
char a[]="String";
char *p="New Sring";
char *Temp;
Temp=a;
a=malloc(strlen(p) + 1);
strcpy(a,p); //Line number:9//
p = malloc(strlen(Temp) + 1);
strcpy(p,Temp);
printf("(%s, %s)",a,p);
free(p);
free(a);
} //Line number 15//

a) Swap contents of p & a and print:(New string, string)
b) Generate compilation error in line number 8
c) Generate compilation error in line number 5
d) Generate compilation error in line number 7
e) Generate compilation error in line number 1

Ans. (b)
23. In the following code segment what will be the result of the function,
value of x , value of y
{unsigned int x=-1;
int y;
y = ~0;
if(x == y)
printf("same");
else
printf("not same");
}

a) same, MAXINT, -1
b) not same, MAXINT, -MAXINT
c) same , MAXUNIT, -1
d) same, MAXUNIT, MAXUNIT
e) not same, MAXINT, MAXUNIT

Ans. (a)
24. What will be the result of the following program ?
char *gxxx()
{static char xxx[1024];
return xxx;
}

main()
{char *g="string";
strcpy(gxxx(),g);
g = gxxx();
strcpy(g,"oldstring");
printf("The string is : %s",gxxx());
}

a) The string is : string
b) The string is :Oldstring
c) Run time error/Core dump
d) Syntax error during compilation
e) None of these

Ans. (b)
25.  Find the output for the following C program
main()
{
char *p1="Name";
char *p2;
p2=(char *)malloc(20);
while(*p2++=*p1++);
printf("%s\n",p2);
}

 Ans. An empty string
26.  Find the output for the following C program
main()
{
int x=20,y=35;
x = y++ + x++;
y = ++y + ++x;
printf("%d %d\n",x,y);
}

Ans. 57 94
27.  Find the output for the following C program
main()
{
int x=5;
printf("%d %d %d\n",x,x<<2,x>>2);
}

Ans. 5 20 1
28 Find the output for the following C program
#define swap1(a,b) a=a+b;b=a-b;a=a-b;
main()
{
int x=5,y=10;
swap1(x,y);
printf("%d %d\n",x,y);
swap2(x,y);
printf("%d %d\n",x,y);
}
int swap2(int a,int b)
{
int temp;
temp=a;
b=a;
a=temp;
return;
}

Ans. 10 5

29 Find the output for the following C program

main()
{
char *ptr = "Ramco Systems";
(*ptr)++;
printf("%s\n",ptr);
ptr++;
printf("%s\n",ptr);
}

Ans. Samco Systems

30 Find the output for the following C program

#include<stdio.h>
main()
{
char s1[]="Ramco";
char s2[]="Systems";
s1=s2;
printf("%s",s1);
}

Ans. Compilation error giving it cannot be an modifiable 'lvalue'

31 Find the output for the following C program

#include<stdio.h>
main()
{
char *p1;
char *p2;
p1=(char *) malloc(25);
p2=(char *) malloc(25);
strcpy(p1,"Ramco");
strcpy(p2,"Systems");
strcat(p1,p2);
printf("%s",p1);
}

 Ans. RamcoSystems

32.  Find the output for the following C program given that
[1]. The following variable is available in file1.c
static int average_float;

Ans. All the functions in the file1.c can access the variable

33.  Find the output for the following C program

# define TRUE 0
some code
while(TRUE)
{
some code
}

Ans. This won't go into the loop as TRUE is defined as 0
34. struct list{
       int x;
      struct list *next;
      }*head;

        the struct head.x =100

       Is the above assignment to pointer is correct or wrong ?

Ans. Wrong
35.What is the output of the following ?

      int i;
      i=1;
      i=i+2*i++;
      printf(%d,i);

Ans. 4
36. FILE *fp1,*fp2;
     
      fp1=fopen("one","w")
      fp2=fopen("one","w")
      fputc('A',fp1)
      fputc('B',fp2)
      fclose(fp1)
      fclose(fp2)
     }

     Find the Error, If Any?

Ans. no error. But It will over writes on same file.
37. What are the output(s) for the following ?
38. #include<malloc.h>
      char *f()
      {char *s=malloc(8);
        strcpy(s,"goodbye");
     }

      main()
      {
      char *f();
      printf("%c",*f()='A');     }



39. #define MAN(x,y) (x)>(y)?(x):(y)
      {int i=10;
      j=5;
      k=0;
      k=MAX(i++,++j);
      printf(%d %d %d %d,i,j,k);
      }

Ans. 10 5 0
40.
void main()
{
int i=7;
printf("%d",i++*i++);
}

Ans: 56





100 keyboard shortcuts
<br>CTRL+C (Copy) 
<br>CTRL+X (Cut) 
CTRL+V (Paste) 
<br>CTRL+Z (Undo)
<br>DELETE (Delete)
SHIFT+DELETE (Delete the selected item permanently without placing the item in the Recycle Bin)
CTRL while dragging an item (Copy the selected item) 
CTRL+SHIFT while dragging an item (Create a shortcut to the selected item)
F2 key (Rename the selected item)
CTRL+RIGHT ARROW (Move the insertion point to the beginning of the next word)
CTRL+LEFT ARROW (Move the insertion point to the beginning of the previous word)
CTRL+DOWN ARROW (Move the insertion point to the beginning of the next paragraph)
CTRL+UP ARROW (Move the insertion point to the beginning of the previous paragraph)
CTRL+SHIFT with any of the arrow keys (Highlight a block of text)
SHIFT with any of the arrow keys (Select more than one item in a window or on the desktop, or select text in a document)
CTRL+A (Select all)
F3 key (Search for a file or a folder)
ALT+ENTER (View the properties for the selected item)
ALT+F4 (Close the active item, or quit the active program)
ALT+ENTER (Display the properties of the selected object)
ALT+SPACEBAR (Open the shortcut menu for the active window)
CTRL+F4 (Close the active document in programs that enable you to have multiple documents open simultaneously) 
ALT+TAB (Switch between the open items) 
ALT+ESC (Cycle through items in the order that they had been opened)
F6 key (Cycle through the screen elements in a window or on the desktop)
F4 key (Display the Address bar list in My Computer or Windows Explorer) 
SHIFT+F10 (Display the shortcut menu for the selected item) 
ALT+SPACEBAR (Display the System menu for the active window) 
CTRL+ESC (Display the Start menu) 
ALT+Underlined letter in a menu name (Display the corresponding menu) 
Underlined letter in a command name on an open menu (Perform the corresponding command) 
F10 key (Activate the menu bar in the active program) 
RIGHT ARROW (Open the next menu to the right, or open a submenu) 
LEFT ARROW (Open the next menu to the left, or close a submenu) 
F5 key (Update the active window) 
BACKSPACE (View the folder one level up in My Computer or Windows Explorer) 
ESC (Cancel the current task) 
SHIFT when you insert a CD-ROM into the CD-ROM drive (Prevent the CD-ROM from automatically playing) 
Dialog Box Keyboard Shortcuts 
CTRL+TAB (Move forward through the tabs) 
CTRL+SHIFT+TAB (Move backward through the tabs) 
TAB (Move forward through the options) 
SHIFT+TAB (Move backward through the options) 
ALT+Underlined letter (Perform the corresponding command or select the corresponding option)
ENTER (Perform the command for the active option or button)
SPACEBAR (Select or clear the check box if the active option is a check box)
Arrow keys (Select a button if the active option is a group of option buttons) 
F1 key (Display Help)
F4 key (Display the items in the active list)
BACKSPACE (Open a folder one level up if a folder is selected in the Save As or Open dialog box)
m*cro$oft Natural Keyboard Shortcuts 
Windows Logo (Display or hide the Start menu) 
Windows Logo+BREAK (Display the System Properties dialog box) 
Windows Logo+D (Display the desktop)
Windows Logo+M (Minimize all of the windows)
Windows Logo+SHIFT+M (Restore the minimized windows)
Windows Logo+E (Open My Computer)
Windows Logo+F (Search for a file or a folder)
CTRL+Windows Logo+F (Search for computers)
Windows Logo+F1 (Display Windows Help)
Windows Logo+ L (Lock the keyboard) 
Windows Logo+R (Open the Run dialog box) 
Windows Logo+U (Open Utility Manager) 
Accessibility Keyboard Shortcuts 
Right SHIFT for eight seconds (Switch FilterKeys either on or off)
Left ALT+left SHIFT+PRINT SCREEN (Switch High Contrast either on or off)
Left ALT+left SHIFT+NUM LOCK (Switch the MouseKeys either on or off) 
SHIFT five times (Switch the StickyKeys either on or off) 
NUM LOCK for five seconds (Switch the ToggleKeys either on or off)
Windows Logo +U (Open Utility Manager) 
Windows Explorer Keyboard Shortcuts 
END (Display the bottom of the active window) 
HOME (Display the top of the active window) 
NUM LOCK+Asterisk sign (*) (Display all of the subfolders that are under the selected folder) 
NUM LOCK+Plus sign (+) (Display the contents of the selected folder)
NUM LOCK+Minus sign (-) (Collapse the selected folder)
LEFT ARROW (Collapse the current selection if it is expanded, or select the parent folder)
RIGHT ARROW (Display the current selection if it is collapsed, or select the first subfolder)
Shortcut Keys for Character Map
After you double-click a character on the grid of characters, you can move through the grid by using the keyboard shortcuts: 
RIGHT ARROW (Move to the right or to the beginning of the next line)
LEFT ARROW (Move to the left or to the end of the previous line) 
UP ARROW (Move up one row) 
DOWN ARROW (Move down one row) 
PAGE UP (Move up one screen at a time) 
PAGE DOWN (Move down one screen at a time) 
HOME (Move to the beginning of the line) 
END (Move to the end of the line) 
CTRL+HOME (Move to the first character) 
CTRL+END (Move to the last character)
SPACEBAR (Switch between Enlarged and Normal mode when a character is selected)
m*cro$oft Management Console (MMC) Main Window Keyboard Shortcuts 
CTRL+O (Open a saved console) 
CTRL+N (Open a new console) 
CTRL+S (Save the open console)
CTRL+M (Add or remove a console item) 
CTRL+W (Open a new window)
F5 key (Update the content of all console windows)
ALT+SPACEBAR (Display the MMC window menu) 
ALT+F4 (Close the console) 
ALT+A (Display the Action menu) 
ALT+V (Display the View menu) 
ALT+F (Display the File menu) 
ALT+O (Display the Favorites menu) 
MMC Console Window Keyboard Shortcuts
CTRL+P (Print the current page or active pane)
ALT+Minus sign (-) (Display the window menu for the active console window)
SHIFT+F10 (Display the Action shortcut menu for the selected item) 
F1 key (Open the Help topic, if any, for the selected item)
F5 key (Update the content of all console windows)
CTRL+F10 (Maximize the active console window) 
CTRL+F5 (Restore the active console window) 
ALT+ENTER (Display the Properties dialog box, if any, for the selected item)
F2 key (Rename the selected item)
CTRL+F4 (Close the active console window. When a console has only one console window, this shortcut closes the console) 
Remote Desktop Connection Navigation 
CTRL+ALT+END (Open the m*cro$oft Windows NT Security dialog box)
ALT+PAGE UP (Switch between programs from left to right) 
ALT+PAGE DOWN (Switch between programs from right to left) 
ALT+INSERT (Cycle through the programs in most recently used order) 
ALT+HOME (Display the Start menu) 
CTRL+ALT+BREAK (Switch the client computer between a window and a full screen)
ALT+DELETE (Display the Windows menu) 
CTRL+ALT+Minus sign (-) (Place a snapshot of the active window in the client on the Terminal server clipboard and provide the same functionality as pressing PRINT SCREEN on a local computer.) <br>
CTRL+ALT+Plus sign (+) (Place a snapshot of the entire client window area on the Terminal server clipboard and provide the same functionality as pressing ALT+PRINT SCREEN on a local computer.)
m*cro$oft Internet Explorer Navigation 
CTRL+B (Open the Organize Favorites dialog box) 
CTRL+E (Open the Search bar)
CTRL+F (Start the Find utility)
CTRL+H (Open the History bar)
CTRL+I (Open the Favorites bar)
CTRL+L (Open the Open dialog box)
CTRL+N (Start another instance of the browser with the same Web address)
CTRL+O (Open the Open dialog box, the same as CTRL+L)
CTRL+P (Open the Print dialog box)
CTRL+R (Update the current Web page)
CTRL+W (Close the current window