Sie sind auf Seite 1von 19

C ++ Questions

Q1.

You are developing an application , part of which requires opening a file named Reserved.tst in the input mode. You have incorporated an object named inobj of class ifstream. Which code segment would you implement in your application to accomplish your requirements? A. #include<iostream.h> int main() { ifstream outobj(reserved.tst); int i,j,k; inobj>>i>>j>>k; cout<<i<< <<j<< <<k; return 0; } Which of the following is true? St. A : the get and put pointers specify the location in the file where the next read and write operation will occur . St. B: the seekg() member ftn takes only one argument as the reference in the file where the pointer has to be positioned.

Q. 2,

Q .3 Which of the following is not a characteristic of an object. Q4. Analuze the following code : #include<iostream.h> class generic { public : generic() { cout<<marshell\n: } ~generic() { cout<<Andromeda\n }; class derived : public generic { public : derived() { cout<<Stanger\n; } ~derived() { cout<<Helo All\n; }

-1-

}; int main() { derived ob; return 0; } What will be the output of the above code? Q 4. Stella works in the railway reservation dept. passengers fill the booking request form with the details of the journey like the train number, Destinition,date etc. She then books the details .. Identify the attributes of the passenger class?

Q . 5. which of the following statements is true? St A: By implementing operator overloading , you can change the precedence of execution for operators/ St. B: by implementing operator overloading , you cannot change the number of operands of the operators. Q. 6. Which access specifiers are supported in C++? Q 7. You are designing an software app. For an acconting company. You need to develop a class, whose member var. should not be accessible to the ftns. Outside the class. Which scope will be applied here. Q 8. class Emp { int static CompanyID; //Code to accept the supplier details static void print() { //code to print the supplier details } int CompanyID=3323; Wht will be the o/p? Q. 9 . wht will be the O/p of following code? Class A { public virtual void show()=0; }; class B : public void display() { cout<<B; };

-2-

class C: public show() { cout<<C } }; Q 10. which of the following operator is used to resolve the ambiguity due to multiple inheritance?. Q. 11. class myclass { int data; static int staticVar; public : static void display() { cout<<Static var=<<staticVar; cout<<data= <<data; } Wht wil be the O/p of this code? Q.12.How to create an object named projrec of ofstream class and link it with a file named market.projrec? Q13.Which of the st.s are correct? A : An array has a fixed size that cannot be changed later. B : The elements of an array may be of different data types. Q.14. Which of operators operate on one operand? Q15. Which of the st. is true : A : The open mode bits specify the methods of opening a file B.: Open mode bits include app and ate. C: Open mode bits are defined in the ios class D: The open mode cannot specify if a file is open for reading or Q16.

writing.

Consider the following statements : A : The generalization can be implemented using an abstract superclass , a conceptual class which acts as a base from which other classes are inherited, B: in generalization , multiple classes inherit from the same super class and have a consistent interface. Which of the option is true?

-3-

Q 17. Consider the code: #include<iostream.h> int main() { ofstream outobj(MyFile.tst); cout<<Success Tastes sweet after defeat; ifstream in(MyFile.tst); char str[50]; in>>str; cout<<str<<endl; return 0; } What wil be the output of the above code ? Q 18. Which of the following statements are true : A : A file pointer always contains the address of the file B :There are two reference points defined in the ios class specifying the beginning and the and of the class. Q 19. Predict the O/P of the code : #include<iostream> class board { int length,breadth; public : board() {} board(int ln,int br) { length=ln; breadth=br; }

} void display() { cout<<length<< ; cout<<breadth<<\n; } board operator +(board op2); board operator ++(); board operator =(board op2); } board :: operator +(board op2) { board temp; temp.length=op2.length+breadth; temp.length=op2.length+breadth; return temp; }

-4-

} int main() { board B1(15,25),B2(10,15); ++B1; B1.display(); B2=++B1; B1.display(); B2.display(); Return 0; }

board :: operator=(board op2) { length=op2.board; breadth=op2.breadth; return *this; } board :: operator ++() { length++; breadth++; return * this; }

Q 20. Consider the following statement : int x= 8+(5*(4+2)-10%3)-20 What will be the value of x ? Q21 . Consider the following code snippet? For( int I=0;I<5;I++) For(int j=0;j<3;j++) If(j==1) Table [I][j]=1; Else Table [I][j]=0; What will be the O/P? Q22 Consider the following code snippet? Class A { public :virtual void show()=0; }; class B:public A { public void display() { cout<<B; } };

-5-

class C : public A { public void show() { cout<<C; } }; Which Statement is erroneous ? Q23 Q24 Which operator is used to resolve the ambiguity that arises due to Multiple Inheritance? You need to develop an application to manage the entry records of the customers in an amusement park. The first customer entering the park will get the maximum discount on the ticket amount. The customer records are maintained in a file named Cust.ent. Your application should read the records from the file and arrange them in reverse order. Which method will you implement to reversae the records?

Q25. When the assignment operator is overloaded , which of the following option is true : 1. The result of the calculation is assigned to both the objects which are left and right to the operator. 2. The result of the arithmetic operator must be returned. 3. The result of the arithmetic calculation is assigned to the object which is left to the operator. 4. The result of the arithmetic calculation is assigned to the object which is right to the operator Q 26 . When a function exists in different forms it is said to be Static or dynamic polymorphism ? Q27. You need to declare a function in the base class which can be redefined in the baseclass. The function has no definition in the base class. How would you achieve this ? Q28. Consider the following code snippet : Class Employee { }; class Manager : protected Employee { }; You have defined a class Employee but the body of the class is not defined. You have also defined a class named Manager and the class Employee is inherited in protected mode. What happens when these classes are compiled?

-6-

Q29. Which of statements are true ? A : Like static variables, you can also declare a function to be static B : Static functions can only access static variables and not non-static variables . Q30. Consider the snippet ? Int iVar; Cin ivar; And the user input is <space>260PI98. Which value iVal will contain? Q31. Which of the following is not a fundamental data type in C++? Int, float,string,char. Q32.#include<iostream.h> Void main() { int I=5; int j=10; for(;I<j;I++) { j--; } cout<<I<<endl; } Predict the O/P? Q 33. Which of the foolowing is not a valid class name ? 1. MyClass! 2. [My_Class] 3. My_Class 4. 1stMytclass Q34. You are developing an application named textpro, par of which prompts the user to input 26 characters , accepts them , and displays it on the screen .Which code segments will you incorporate in the application to accomplish the requirements?

Q35. You are developing an application that involves arrays implementation. You are implementing multiple arrays in your application and are trying to initialize an array with an other array. How will this effect the execution of your program?

-7-

Q 36. Consider the following snippet? Char x[10]; Char *y=abcdefgh; x=y; What will be the value of x[0]? Q 37. Consider the following snippet? int a[8]={43,21,86,23,14,22,71,12}; int *x,ans; x=&a[8]; ans=*x; What will be the value of the variable ans? Q 38. Consider the following statements ? A: char str[20]=Hi; strcat(str,Hello) B: strcat(Hi,Hello); Which of the following are valid? Q 39. Consider the following snippet? #include<iostream.h> int x(0xffff); int &b=x; int &x=b; void main() { } What will be the O/P of the following code ? Q 40. Consider the following statements ? A : the members of a struct are private by default . B : the members of a class are public by default. Which of the following are true ?. Q 41. Which statement is true about the this pointer ? Ans : The this pointer is used by the member functions to find out the address of the object of which it is a member i.e . this pointer points to the object itself..

-8-

Q 42 . Consider the following statements ? A : The most common use of stack is to store the function arguments and address whenever a function calls another function . B : Queues are basically used in the multi user environment for the proper scheduling of jobs which are sent for execution . Which of the following are valid? Q 43. Consider the following statements ? A : In linked lists , memory is allocated whenever required , so it is necessary to know number of elements in advance and allocate memory . B : Insertion and Deletion in an array can be handled more efficiently because there is no manipulation of pointers. Which of the following are valid? Q 44. Consider the following snippet? #include<iostream.h> void main() { int var1,var2; var1= 5.5+2.3; var2=(int)2.3+5.5; cout<<var1; cout<<var2; } What will be the O/P of the above code ? Q 45. Consider the following snippet? #include<iostream.h> int main() { int num1; int *ptr1,**ptr2; num1=65; ptr1=&num1; ptr2=&ptr1; cout<<*ptr1; cout<<**ptr2; } What will be the O/P of the above code ?

-9-

Q 46. Consider the following statements ? A : The current position of the get ans put pointers in a file can be found by the tellg() functions respetively. B : Random access of data files can be done using the seekg() and seekp() member functions . Which of the following are valid? Q 47. Consider the following snippet? #include<iostream> class A { public: A() { cout<<Inside As Cons<<endl; } }; class B : public A { int i; public : B() { cout<<Inside Bs Cons; } B(int j) { i=j; cout<<Inside Bs Cons with param<<i<<endl; } }; int main() { A a(); B b(100); } What will be the O/P of the above code ?

- 10 -

Q.48. Consider the following snippet? Void main() { float me = 1.1; double you = 1.1; if(me==you) cout<<"I love U"; else cout<<"I hate U"; } What will be the O/P of the above code ?

Q 49. . Consider the following snippet? Void main() { int i=3; switch(i) { default:cout<<"zero"; case 1: cout<<"one"; break; case 2:cout<<two"; break; case 3: cout<<"three"; break; } } What will be the O/P of the above code ?. Q 50. . Consider the following snippet? class Sample { public: int *ptr;

Sample(int i) { ptr = new int(i); }

- 11 -

~Sample() { delete ptr; } void PrintVal() { cout << "The value is " << *ptr; } }; void SomeFunc(Sample x) { cout << "Say i am in someFunc " << endl; } int main() { Sample s1= 10; SomeFunc(s1); s1.PrintVal(); } What will be the O/P of the above code ?. Q 51. Which is the parameter that is added to every non-static member function when it is called? Q 52 Consider the following snippet? class base { public: int bval; base(){ bval=0;} }; class deri:public base { public: int dval; deri(){ dval=1;} }; void SomeFunc(base *arr,int size) { for(int i=0; i<size; i++,arr++) cout<<arr->bval; cout<<endl; }

- 12 -

int main() { base BaseArr[5]; SomeFunc(BaseArr,5); deri DeriArr[5]; SomeFunc(DeriArr,5); } What will be the O/P of the above code ? Q 53. Consider the following snippet? class base { public: void baseFun(){ cout<<"from base"<<endl;} }; class deri:public base { public: void baseFun(){ cout<< "from derived"<<endl;} }; void SomeFunc(base *baseObj) { baseObj->baseFun(); } int main() { base baseObject; SomeFunc(&baseObject); deri deriObject; SomeFunc(&deriObject); } What will be the O/P of the above code ? Q . 54 . Consider the following snippet? void main() { int a, *pa, &ra; pa = &a; ra = a; cout <<"a="<<a <<"*pa="<<*pa <<"ra"<<ra ; } What will be the O/P of the above code ?

- 13 -

Q .55. Consider the following snippet? class some { public: ~some() { cout<<"some's destructor"<<endl; } }; void main() { some s; s.~some(); } What will be the O/P of the above code ? Q 56. Consider the following snippet? #include <iostream.h> class fig2d { int dim1; int dim2; public: fig2d() { dim1=5; dim2=6;} virtual void operator<<(ostream & rhs); };

void fig2d::operator<<(ostream &rhs) { rhs <<this->dim1<<" "<<this->dim2<<" "; } class fig3d : public fig2d { int dim3; public: fig3d() { dim3=7;} virtual void operator<<(ostream &rhs); }; void fig3d::operator<<(ostream &rhs) { fig2d::operator <<(rhs); rhs<<this->dim3; }

- 14 -

void main() { fig2d obj1; // fig3d obj2; obj1 << cout; // obj2 << cout; } What will be the O/P of the above code ? Q 57. Consider the following snippet? class complex{ double re; double im; public: complex() : re(1),im(0.5) {} bool operator==(complex &rhs); operator int(){} }; bool complex::operator == (complex &rhs){ if((this->re == rhs.re) && (this->im == rhs.im)) return true;

else }

return false;

int main(){ complex c1; cout<< c1; } What will be the O/P of the above code ? Q 58. Consider the following snippet? main() { char string[]="Hello World"; display(string); } void display(char *string) { cout<<string; } What will be the O/P of the above code ?

- 15 -

Q 59. Consider the following snippet? main() { cout<<"\nab"; cout<<"\bsi"; cout"\rha"; } What will be the O/P of the above code ? Q 60. Which of the following statements is true? St .A : The Compiler is a software that translates a program written in a language like C++ into machine language and the file containing the translated program is called the object code. St B : Linking is a process of combing the object code with the object code of the function you use and adds some standard startup code to produce a run time version of the source code. Q 61.#include<header.h> statement in a C++ is known as __________________.? Q62. Consider the following statements : A : Static functions and non-static functions can only access static variables B : Friend functions may be declared a friend by a class , in which case the function may be directly access the private member attributes and methods of the class objects. Which of the above statements are valid ?

Q 63. 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.

- 16 -

Q 64. Consider the following snippet ? void main() { int i; for(i=1;i<4,i++) switch(i) case 1: cout<<i;break; case 2:cout<<ibreak; case 3:cout<<i;break; }

What will be the O/P of the above code ?

Q 65. Consider the following snippet ? void main() { int count=10,*temp,sum=0; temp=&count; *temp=20; temp=&sum; *temp=count; cout<<count<<\t<<*temp<<\t<<sum<<endl; } What will be the O/P of the above code ? Q 66. Consider the following snippet ? char *foo() { char result[100]; strcpy(result,"anything is good"); return(result); } void main() { char *j; j=foo() cout<<j<<endl; } What will be the O/P of the above code ?

- 17 -

Q 67. C++ is a _______________language . Q 68. Consider the following snippet ? #include<iostream.h> main() { char s1[]="NIIT"; char s2[]="Faridabad"; s1=s2; cout<<s1<<endl; } What will be the O/P of the above code ?

Q 69. Consider the following snippet ? main() { int x=5,y=10; swap(x,y); cout<<x<<\t<<y<<endl; } int swap(int a,int b) { int temp; temp=a; b=a; a=temp; return; } What will be the O/P of the above code ? Q 70. Consider the following statements : Suppose there are 100 records in a file. iFil is an object of class ifstream A : iFil.seekg(-50,ios::end); B : iFil.seekp(50,ios::beg);

How above statements will affect the read-write operations in the file?

- 18 -

- 19 -

Das könnte Ihnen auch gefallen