Instruction: When specified, you may choose more than one answer; otherwise, choose ONE answer for each question. Choose the answer(s) by circling it/them on the Answer Sheet provided. Questions 1-12 are allocated 1 mark each; questions 13-36 are allocated 2 marks each. The total marks are 60. Q1 Given the C++ declaration statement below, which of the following represents the value of exforsys? int exforsys[10]; a) &exforsys[9] b) &exforsys[0] c) *exforsys[9] d) *exforsys[0] Q2 Given that k is an integer array starting at address 3000, kptr is a pointer to the array as declared by the C++ statements below. If each integer is stored in 4 bytes of memory, what is the value of kptr+2?. int k[20]; int *kptr=k; a) 3000 b) 3004 c) 3008 d) 3012 Consider the following C++ program named CBWT_3.exe that is executed inside the Command Prompt window using the command line CBWT_3 EIE236 programming test. Answer questions Q3 - Q4. int main(int argc, char *argv[]) cout << argv[argc-1]; Q3 What is the value of the variable argc after executing the above program? a) 5 b) 4 c) 3 d) 2 e) 1 Q4 What will be shown on the screen after executing the above program? a) CBWT b) CBWT_3 c) EIE236 d) programming e) test 1
Q5 Which of the following is/are true for describing the function prototype char * GetName()? (You may choose more than one answer.) a) GetName() is a pointer. b) GetName() may return a character pointer s value. c) GetName() requires no input parameters. d) GetName() may return the address of a character array in the heap. e) GetName() may return the address of a character in the heap. 1A Q6 Which of the following prototype describes the function that converts an unmanaged character array to an integer? a) int System::Convert::ToInt32(String); b) int System::Convert::ToInteger(String); c) int atoi (char *); d) int atof (char *); e) int itoa (char *); Q7 Which of the following statement about GDI+ is false? a) It involves a set of namespaces and library functions for the rendering of 3D graphics. b) It provides support for colors, pens, fonts, image transformations, etc. c) By using it, outputs of graphics can be shown on screens and printers. d) When starting a Windows Forms project in Visual C++ 2008, GDI+ has been included by default. Q8 Consider the following event handler for a button in a managed application with GUI: private: System::Void button1_click(system::object ^ sender, System::EventArgs ^ e) no = 1; Form2 ^ F2 = gcnew Form2(no); F2->ShowDialog(); //Line 5 Which of the following description(s) about Line 5 is/are correct? (You may choose more than one answer.) a) A modal form will be shown on the screen. b) A form object will be instantiated. c) We cannot access other forms of this application unless we close the form generated by Line 5. d) We can access other forms of this application without closing the form generated by Line 5. e) Line 5 contains error(s) and cannot be successfully compiled. Q9 Which of the following statement(s) is/are declaring variable(s) of type supplied by the.net Framework Class Library? (You may choose more than one answer.) a) String ^ message; b) float i; c) char * message; d) Char text; e) char text[10]; Q10 Which of the following must return a number between 50 and 69 inclusively? a) rand()/20; b) rand()%70-50; c) rand()%70-1; d) rand()%20 + 50; 2
1A Q11 Consider the following event handler for a button in a managed application with GUI: private: System::Void button1_click(system::object^ sender, System::EventArgs^ e) String^ name = "Matt"; String^ group = "Gp. 84"; char* welcome = "Hello, "; strcat(welcome, name); // line 6 What should be the code in line 6 in order to make the output below shown on the screen on clicking the button? a) MessageBox::Show(welcome+name, group, MessageBoxButtons::OKCancel); b) MessageBox::Show(group, welcome+name, MessageBoxButtons::OKCancel); c) MessageBox::Show(welcome, group, MessageBoxButtons::OKCancel); d) MessageBox::Show(group, welcome, MessageBoxButtons::OKCancel); Q12 On executing the following C++ program, what will be the output shown on the screen? int y[8] = 8, 3, 2, 1, 4, 6, 7, 0; int *s; s = y + 2; cout << *s << " " << s[3] << endl; a) 8 1 b) 2 6 c) 2 1 d) 8 4 e) The program contains error(s) and cannot run. Instruction: Each of the following questions below takes TWO marks. When specified, you may choose more than one answer; otherwise, choose ONE answer for each question. Q13 On executing the following C++ program, what will be the output shown on the screen? void greeting (int x, int y) if (x > 5) if (y < 3) cout << "Hello "; else cout << "Hi "; cout << "Bye " ; if (x > 5) if (y < 3) cout << "Hello " ; else 3
cout << "Hi " ; cout << "Bye "; int main () greeting (3, 2); greeting (1, 1); a) Bye Bye b) Hi Bye Hi Bye Hi Bye Hi Bye c) Bye Hi Bye Bye Hi Bye d) Hi Bye Hi Bye e) The above program contains error(s) and cannot be executed. Consider the following C++ function that opens an input file. Then answer questions Q14 - Q16. int Count(char * filename) char ch; int count = 1; ifstream fin(filename); if( ) //Line 6 cout<<"no such file!"<<endl; while(fin.get(ch)) if( ch == '\n') count++; fin.close(); return count; Q14 Which of the following header file(s) should be included into the project before the statement ifstream fin(filename); can be successfully compiled? a) iostream b) fstream c) string.h d) Both a) and b) Q15 What should be put as the missing condition in Line 6 if the above function is supposed to show the message No such file! when the input file does not exist? a) filename b) fin c)!filename d)!fin Q16 Which of the following can be the job done by the above function? a) Return the number of characters in a file. b) Return the number of lines in a file. c) Return the number of words in a file. d) No job can be done as the function contains error(s). 2A Consider the following C++ program and answer questions Q17 - Q18. double *aa = new double[3];//assume aa store the address 0x00537ae0 aa[2] = 10; double *b = aa + 2; //Line 3 cout <<*b <<endl; delete [] aa; Q17 Which of the following is/are true for the above program? (You may choose more than one answer.) 4
a) The address of the memory storing the value of b is 0x00537ae8. b) Memory leak will occur after executing this program. c) The statement commented as Line 3 can be replaced by *b=10; d) The above program cannot be successfully compiled. 2E Q18 What will be shown on the screen on executing the above program? a) 0x00537AE8 b) 0x00537AF0 c) 10 d) Irrelevant as the above program cannot be successfully compiled. Consider the following 3 program segments for a managed application. Then answer questions Q19 - Q21. 1 using namespace System::Runtime::InteropServices; 2 #include <string.h> 3 private: System::Void button1_click(system::object^ sender, System::EventArgs^ e) 4 char message[100] = " Welcome "; 5 String ^ tbstr = textbox1->text; 6 char *name = (char*)marshal::stringtohglobalansi(tbstr).topointer(); 7 strcat(message,name); 8 String^ Mmge=Marshal::PtrToStringAnsi((IntPtr)message); 9 String^ title = " Hello "; 10 MessageBox::Show(Mmge,title,MessageBoxButtons:: OKCancel); 11 Marshal::FreeHGlobal((IntPtr)name); 12 Q19 Which of the following line(s) will generate compilation error(s) if line 1 is missing? (You may choose more than one answer.) a) Line 6 b) Line 7 c) Line 8 d) Line 10 e) Line 11 Q20 Which of the following line(s) will generate compilation error(s) if line 2 is missing? (You may choose more than one answer.) a) Line 4 b) Line 5 c) Line 6 d) Line 7 e) Line 8 Q21 What is the output shown on the screen if the application executes the above event handler when the user types nothing into the text box of handle textbox1? a) b) c) d) e) A run-time error message will be shown. 5
Q23 What will be seen on the screen on executing the program below? char a[10]="testing"; char b[10]="hello"; char *t = a; char *f = b; while (*t++=*f++); cout<<a<<" "; cout<<b<<endl; a) testing hello b) testing testing c) hello hello d) Many characters as the application is trapped into an infinite loop. 2E Consider the following C++ program and answer questions Q24 - Q25. #include <fstream> ofstream test1file("test1.txt"); test1file << "1234567890"; test1file.seekp(4); test1file << "test"; cout << test1file.tellp(); test1file.close(); 6
Q24 On executing the above C++ program, what will be shown on the screen? a) 0 b) 4 c) 8 d) 9 e) 10 Q25 On executing the above C++ program, what will be the content of the file test1.txt? a) 123test b) 1234567890test c) 1234test90 d) 123test890 e) 1234test Q26 What will be shown on the screen on executing the program below? #include <fstream> ofstream test1file("test1.txt"); test1file << "1234567890"; test1file.close(); ifstream tin("test1.txt"); char ch; for (int i=2; i<5; i++) tin.get(ch); cout << ch; tin.close(); a) 123 b) 234 c) 2345 d) 345 Q27 On executing the following C++ program, what will be shown on the screen? #include <fstream> ofstream fout("myfile.txt"); fout << "This line is written to file. "; fout.close(); char fname[]="myfile.txt"; ofstream fout2(fname,ios::ate); fout2 << "Wonderful world. "; fout2.close(); ifstream fin("myfile.txt"); char ch; while (fin.get(ch)) cout << ch; fin.close(); a) This line is written to file. Wonderful world. b) Wonderful world. ten to file. c) Wonderful world. d) Wonderful world. This line is written to file. 2A Assume that on executing the following C++ program, the user inputs the 3 characters a, s and p and then presses the enter key. Answer the questions Q28 - Q29. 7
Q28 char input; cin>>input; // Assume the user inputs "asp" while ((input)!= 'p') // line 7 cout << "input: " << input<< ' '; What will be the output seen on the screen? a) input: a input: s input: p b) input: a input: s c) input: s d) The application is trapped in an infinite loop and continuously displays many characters. Q29 If line 7 is changed to while ((input=cin.get())!= 'p'), what will be the output shown on the screen? a) input: a input: s input: p b) input: s input: p c) input: s d) The application is trapped in an infinite loop and continuously displays many characters. 2A Consider the following event handler in a managed application with GUI. Then answer questions Q30 - Q31. private: System::Void Form1_Paint(System::Object ^ sender, System::Windows::Forms::PaintEventArgs ^ e) Graphics ^g = e->graphics; Drawing::Rectangle Head = Drawing::Rectangle(30, 30, 50, 50); g->fillellipse(brushes::yellow, Head); // Line 6 Q30 Which of the following description(s) is/are correct? (You may choose more than one answer.) a) The size of the drawn rectangle is 30 30. b) The size of the drawn rectangle is 50 50. c) The x and y coordinates of the drawn rectangle are for the bottom-left corner. d) The x and y coordinates of the drawn rectangle are for the top-left corner. Q31 Which of the following statement(s) about the above event handler is/are correct? (You may choose more than one answer.) a) The event handler can be called by this->invalidate(); b) The event handler can be called by Form1->Invalidate(); c) Line 6 can be changed to e->graphics->fillellipse(brushes::yellow, Head); d) A timer must be present in order to run this event handler. Q32 On executing the following C++ program in the Command Prompt window, the user enters test 1 2 3 4 in the command line. What will be shown on the output screen? #include <stdlib.h> int main(int argc, char *argv[]) int temp=0; 8
for (int i = 1; i<argc; i++) temp=temp+atoi(argv[i]); cout<<temp<<endl; a) 0 b) 10 c) 24 d) 6 Q33 Consider the following Windows Forms application: If the event handler of the ok button is given as follows, what will be the output shown on the screen? private: System::Void button1_click(system::object^ sender, System::EventArgs^ e) int no1 = 5; String^ no2 = "2"; textbox1->text = no1+no2; a) b) c) d) e) The program contains error(s) and cannot run. 2E Q34 On executing the following C++ program, what will be shown on the screen? 9
int i=0; for(int j=0; j<9; j++) for(int j=0; j<9; j++) i++; cout<<i<<endl; a) 0 b) 80 c) 81 d) The program contains error(s) and cannot run. 2A Q35 On executing the following C++ program, what will be shown on the screen? int GPA=4; int *p1,*p2; p1=0; p2=0; p1=&gpa; p2=&gpa; *p1 = 2; *p2 = 3; cout<<*p1<<","<<*p2<<endl; a) 2,3 b) 4,4 c) 3,3 d) The program contains error(s) and cannot run. 2A Q36 On executing the following C++ program, what will be shown on the screen? #include <string.h> const int MaxLength = 80; char string1[] = "I like Maths and Programming."; char string2[] = "I like C++ and English."; cout<<strlen(string1)<<", "; cout<<strlen(string2)<<", "; strncpy(string1,string2,11); cout << string1 << endl; a) 30, 24, I like C++ a and programming. b) 30, 24, I like C++ s and programming. c) 29, 23, I like C++ a and programming. d) 29, 23, I like C++ s and programming. 2E - End - 10