R4R
Right Place For Right Person TM
R4R C++

 


Totel:235 Click: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

C++ Interview Questions And Answers

Page 10

Questions 46 
What will be the output of the following code?
void main (){
 int i = 0 , a[3] ;
a[i] = i++;
printf ("%d",a[i]) ;
} 

Answer
The output for the above code would be a garbage value. In the statement a[i] = i++; the value of the variable i would get assigned first to a[i] i.e. a[0] and then the value of i would get incremented by 1. Since a[i] i.e. a[1] has not been initialized, a[i] will have a garbage value.

Questions 47 Why doesn't the following code give the desired result?

Answer
:int x = 3000, y = 2000 ; long int z = x * y ; Ans:Here the multiplication is carried out between two ints x and y, and the result that would overflow would be truncated before being assigned to the variable z of type long int. However, to get the correct output, we should use an explicit cast to force long arithmetic as shown below: long int z = ( long int ) x * y ; Note that ( long int )( x * y ) would not give the desired effect.

Questions 48 
Why doesn't the following statement work?
char str[ ] = "Hello" ;
strcat ( str, '!' ) 

Answer
The string function strcat( ) concatenates strings and not a character. The basic difference between a string and a character is that a string is a collection of characters, represented by an array of characters whereas a character is a single character. To make the above statement work writes the statement as shown below: strcat ( str, "!" ) ;

Questions 49 How do I know how many elements an array can hold?

Answer
main( ){ int i[32767] ; float f[16383] ; char s[65535] ; }

Questions 50 How do I write code that reads data at memory location specified by segment and offset? 

Answer
Use peekb( ) function. This function returns byte(s) read from specific segment and offset locations in memory. The following program illustrates use of this function. In this program from VDU memory we have read characters and its attributes of the first row. The information stored in file is then further read and displayed using peek( ) function. #include <stdio.h> #include <dos.h> main( ){ char far *scr = 0xB8000000 ; FILE *fp ; int offset ; char ch ; if ( ( fp = fopen ( "scr.dat", "wb" ) ) == NULL ) { printf ( "\nUnable to open file" ) ; exit( ) ; } for ( offset = 0 ; offset < 160 ; offset++ ) fprintf ( fp, "%c", peekb ( scr, offset ) ) ; fclose ( fp ) ; if ( ( fp = fopen ( "scr.dat", "rb" ) ) == NULL ) { printf ( "\nUnable to open file" ) ; exit( ) ; } for ( offset = 0 ; offset < 160 ; offset++ ) { fscanf ( fp, "%c", &ch ) ; printf ( "%c", ch ) ; } fclose ( fp ) ; }


Goto Page:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
Share |

CPP Objective Questions And Answers

CPP Objective Questions And Answers

CPP Interview Questions And Answers

CPP Interview Questions And Answers


R4R,CPP Objective, CPP Subjective, CPP Interview Questions And Answers,CPP,CPP Interview,CPP Questions ,CPP Answers

New Updates

R4R
R4R
R4R
R4R
R4R
R4R
R4R
R4R