1 Student Name : Student EID : Time Allowed : 50 minutes Question 1: What is the output of the following program? (1 x 6 marks). (1) int a = 0; a>0?a++:--a; cout << "a is \"" << 'a' << "\""; (2) int a = 10.5; cout<< a/3; (3) int a=0, b=0; a = (a = 1) + (b = 2); cout<< a; (4) int i,j,k; k = (i=1) && (j=1); k = (i=0) && (j=2); k = i || (j=3); k = (i=2) || (j=4); cout<<j; (6) int a=2,b=1; if (a=1) if (b=1) cout <<1; else cout <<2; cout <<3; (5) int x=5; if (x!=5); x=3; cout << x; Your Answers: (1) a is "a" (3) 3 (2) 3 (4) 3 (5) 3 (6) 13 Question 2: Multiple-Choice Questions (Select exactly ONE choice for each question). (0.5 x 4 marks). 1) What is the largest integer value that can be represented by an unsigned char variable? (A) 28 (B) 28 − 1 (C) 27 (D) 27 − 1 2) What is output of the following code: cout<< (char)( ‘b' + 1) ? (A) 𝟗𝟗 (B) 2𝟓𝟓 (C) a (D) c (C) 𝟏 (D) 𝟐 3) What is output of the following code: cout<<1/2 ? (A) 0. 𝟓 (B) 𝟎 4) Let x denote a variable, what is the correct C++ expression for the following description: “x is larger than or equal to 0 and x is smaller than 256”? (A) 0 =< x & x < 256 (B) 0 =< x < 256 (C) x>=0 && x < 256 (D)0=<x && x <= 255 Your Answers: 1) B (2) D (3) B (4) C 2 Question 3: The following program outputs the smaller user input. Some code contains syntax errors. Write the correct codes in the right column. Marks will be deduced for incorrect attempts. (4 marks). Line number Program Code Correct Code 1 include <iostream> #include <iostream> //0.5 2 using namespace std; 3 void main 4 void main() //0.5 {//0.5 5 int x; y; z; int x, y, z;//0.5 6 cin >> x[0] >> y[0]; cin>>x>>y;//0.5 7 z = (x <= y) ? x , y; Z=(x<=y) ? x : y;//0.5 mark 8 9 cout>>z>>end; cout<<z<<endl;//0.5+0.5 mark } Question 4 (4 marks). Complete the following program that reads in the number of seconds and converts it to hours, minutes and seconds. A sample output of your program is as follows. Please enter the number of seconds : 3601 3601 second(s) = 1 hour(s) 0 minute(s) 1 second(s) #include <iostream> using namespace std; void main() { int hour, min, sec, inSec; cout<<"Please enter the number of seconds: "; cin>>inSec; min = inSec/60; //1 mark hour = min/60; //1 mark min %=60; // 1 mark sec = inSec%60; //1 mark cout<<inSec<<" second(s) = "<<hour<<" hour(s) "<<min<<" minute(s) "<<sec<<" second(s)\n"; } 3 Question 5 (4 marks). Complete the following code to compute the square root of an integer if its square root is also an integer. You cannot use any additional libraries or functions (e.g., sqrt). You may need to use while or for loop and can only use math operators (+, -, *, / and %) and relational, equality, logical operators (>, >=, <, <=, ==, !=, !, &&, ||). #include <iostream> using namespace std; void main() { int x=0,y=0; cout<<"Input a positive number"<<endl; while(x<=0) cin>>x; for(int i=0;i<x;i++)//1 { if(i*i==x) //1 { y=i; //1 break; //1 } } if(y>0) cout<<"Square root of "<<x<<" is "<<y<<endl; else cout<<"Square root of "<<x<<" is not an integer"<<endl; } Question 6 (4 marks, bonus). Complete the following code to compute the square root of a positive number. You cannot use any additional libraries or functions (e.g., sqrt). You may need to use while or for loop and can only use math operators (+ , -, *, / and %) and relational, equality, logical operators (>, >=, <, <=, ==, !=, !, &&, ||). In practice, a computer program cannot obtain the exact square roots of most numbers as it uses only 64 bits to represent a double number. Let 𝑟 denote the exact square root, and 𝑦 denote your result. Please analyze the error 𝑟 − 𝑦 resides in your program. Please discuss the error resides in your program below: Let 𝑟 denote the exact square root, and 𝑦 denote your result. Please analyze the error 𝑟 − 𝑦 resides in your program. 4 #include <iostream> using namespace std; void main() { double x=0,y=0; cout<<"Input a positive number"<<endl; while(x<=0) cin>>x; double min_double=1.0; for(int i=0;i<32;i++) min_double/=2; double lower_bound=0; double upper_bound=x; double difference; do { y=(lower_bound+upper_bound)/2; if(y*y>=x) { upper_bound=y; } else { lower_bound=y; } difference=x-(y*y); if(difference<0) difference=-difference; } while(difference>min_double); if(y>0) cout<<"Square root of "<<x<<" is "<<y<<endl; }