Prog 2 _ Final exam 2014

advertisement
‫المملكة العربية السعودية‬
‫وزارة التعليم العالي‬
‫جامعة المجمعــة‬
‫كلية العلوم بالزلفي‬
‫قسم علوم الحاسب والمعلومات‬
Kingdom of Saudi Arabia
Ministry of Higher Education
Majmaah University
Collage of Science at Al-Zulfi
Computer & Information Dept.
Final Exam First Semester 1435-1436H
Course Name: Programming Lang.(2)
- Course Code: CSI 221 -
Section :138
)2( ‫(نموذج إجابة) امتحان مادة برمجة‬
Time Allowed: 120 minutes
Date: 14/ 03/ 1436 H  5 / 01 / 2015 G.
Number of Pages: 9
Student's Name:......................................................................................................
Student's ID: ............................................................................................................
Instruction:
1. Write your name and your academic identification number fully and clearly.
2. Use blue or black pen to answer, pencils only for drawing.
3. Books and notes are not allowed.
4. Students are not allowed to leave before the lapse of 30 minutes from the beginning
of the exam.
Learning Outcomes
Knowledge
1.1
1.2
a2
1.3
a3
Cognitive Skills
1.4
2.1
2.2
b1
b2
2.3
Question
Outcome covered
I
a2,a3
II
b1,b2
III
Communication, Information
Technology, Numerical
Interpersonal Skills
2.4
3.1
3.2
3.3
c3
Correction
3.4
4.1
d3
V
---
4.3
5.1
5.2
d1
Verification
c3
IV
4.2
Psychomotor
Total
Faculty Member
Course
coordinator
Dr. Wael Khedr Dr. Yosry Azzam
Revision Committee
Name
Final Mark :
Signature
/ 40
Marks:……………………………………………………………………………
2 ‫نموذج‬
1
Question(1) : Multiple Choice Questions------------------------------------(10 marks)
1. A relational operator ( > , < , >= , <=, != )
a. assigns one operand to another.
b. yields a Boolean result.
c. compares two operands.
d. logically combines two operands.
2. The && and || operators
a. compare two numeric values.
b. combine two numeric values.
c. compare two Boolean values.
d. combine two Boolean values.
3. An enumeration brings together a group of
a. items of different data types.
b. related data variables.
c. integers with user-defined names.
d. constant values.
4. A structure brings together a group of
a. items of the same data type.
b. related data items.
c. integers with user-defined names.
d. variables.
5.
void functionA( void );
a. It does not receive any arguments.
b. It could have been written void functionA( );
c. It does not return a value.
d. All of the above
6. A function argument is
a. a variable in the function that receives a value from the calling program.
b. a way that functions resist accepting the calling program’s values.
c. a value sent to the function by the calling program.
d. a value returned by the function to the calling program.
7. When an argument is passed by reference '&'
a. a variable is created in the function to hold the argument’s value.
b. the function cannot access the argument’s value.
c. a temporary variable is created in the calling program to hold the argument’s value.
d. the function accesses the argument’s original value in the calling program.
8. Overloaded functions
a. are a group of functions with the same name.
b. all have the same number and types of arguments.
c. make life simpler for programmers.
d. may fail unexpectedly due to stress.
2
9. A static local variables exist for
a.
b.
c.
d.
the life of a program but are visible only in their own functions.
the life of a program but are visible only in their overloaded functions.
only while the function in which they are defined is executing.
the life of a program.
10. In a class definition, data or functions designated private are accessible
a.
b.
c.
d.
to any function in the program.
only if you know the password.
to member functions of that class.
only to public members of the class.
11. In C++, a function contained within a class is called
a.
b.
c.
d.
a member function.
an operator function.
a class function.
A prototype function.
12. A constructor can specify the return type:
a.
b.
c.
d.
int.
string.
void.
A constructor cannot specify a return type.
13. An expression
a. usually evaluates to a numerical value.
b. indicates the emotional state of the program.
c. always occurs outside a function.
d. may be part of a logical statement.
14. Let int x = y= z=0;
y = z++;
x = ++z;
cout << x << y << z;
a. 2 0 2
b. 2 2 2
c. 0 2 0
d. 0 1 2
15. Let
int x = 3 , y=5;
cout << ( x < y ? x : y );
a.
b.
c.
d.
3
5
5:3
3:5
3
16. int *ptr , x=5 ;
ptr = & x;
*ptr +=1 ;
cout << x;
a. 1
b. 5
c. 6
d. NULL
17. How many times will the following loop print hello?
i = 1;
while ( ++i < = 10 )
cout << "hello";
a. 0
b. 9
c. 10
d. An infinite number of times.
18. const float P = 3.14159F;
it means that
a. P is a constant qualifier cannot be changed.
b. P is a constant value but can be changed.
c. P is a float variable with initial value .
d. P is a static local constant variable .
19. An array element is accessed using
a.
b.
c.
d.
first-in-first-out approach.
the dot operator.
a member name.
an index number.
20. int* Ptr, Grade[7]={1,2,3,4,5,6,7} ;
Ptr = Grade;
cout<< Ptr[5];
a. 5
b. 6
c. 7
d. ERROR
4
Question(2) : [ 10 Marks]
A) Write a structure specification that includes three variables—all of type int—called hrs, mins, and
secs. Call this structure time. [ 2 marks ]
Answer:
struct time
{ int hrs;
int mins;
int secs;
};
B) Write statements that define a variable time2 structure and set the hrs member variable equal to 17
, mins to . 30 , and secs to 10.
[ 2 marks]
time time2;
Another Answer:
time2hrs= 17;
time2mins= 30;
time2secs= 10;
int main(void)
time time2={ 17, 30, 10 };
C) Compute and Print out the total number of seconds represented by this time2 value: Totalsecs =
hours*3600 + minutes*60 + seconds.
[ 2 marks]
Answer:
int main(void)
time time2={ 17, 30, 10};
int Totalsecs = time2hrs* 3600+ time2mins*60 + time2secs;
D) Write a statement that declares an enumeration called Players with the values
Back_defender=5, Back_Left=10, Back_right, and Striker.
[ 2 marks]
Keeper=1,
Answer:
enum Players { Keeper=1, Back_defender=5, Back_Left=10, Back_right, Striker };
E) Define two variables Elshamrany and Eldeaae of enumeration Players and assign them the
values Striker and Keeper, respectively?
[2 marks]
Answer:
Players Elshamrany = Striker , Eldeaae = Keeper ;
5
Question(3) : 3.A) Write the outputs of executing this program
[ 5 Marks]
#include <iostream>
using namespace std;
void order(int&, int&);
int main()
{
int n1=99, n2=11;
int n3=22, n4=88;
order(n1, n2);
order(n3, n4);
cout << “n1=” << n1 << endl; //print out all numbers
cout << “n2=” << n2 << endl;
cout << “n3=” << n3 << endl;
cout << “n4=” << n4 << endl;
return 0;
}
//-------------------------------------------------------------void order(int& numb1, int& numb2)
{
if(numb1 > numb2)
{
int temp = numb1;
numb1 = numb2;
numb2 = temp;
}
}
The outputs are:
n1 = 11
n2 = 99
n3 = 88
n4 = 22
6
3.B) Write the outputs of executing this program
[ 5 Marks]
#include <iostream>
using namespace std;
float getavg(float);
int main()
{
float data=1, avg;
while( data != 0 )
{
cout << “Enter a number: “;
cin >> data;
avg = getavg(data);
cout << “New average is “ << avg << endl;
}
return 0;
}
//-------------------------------------------------------------float getavg(float newdata)
{
static float total = 0; //static variables are initialized
static int count = 0; // only once per program
count++; //increment count
total += newdata; //add new data to total
return total / count;
}
The outputs are:
Enter a number: 10
New average is 10
total is 10, count is 1
Enter a number: 20
New average is 15
total is 30, count is 2
Enter a number: 30
New average is 20
total is 60, count is 3
When Enter a number: 0
Exit from program
7
Question(4) : [ 10 Marks]
A) Create a class called Employee that contains two variables a name (an object of class string) and an
employee ID (type long). Include a two public member functions called getdata() to get data from the
user for insertion into the object, and another function called putdata() to display the data.
[ 3 marks]
class Employee
{ private:
string name;
long ID;
Public:
void getdata(void) ;
void putdata(void) ;
};
B) Define the public function void getdata() outside class definition to get data from the user
for insertion into the object?
[2 marks]
void Employee :: getdata(void)
{ cout<< "enter Employee name:";
cin>> name;
cout<< "enter Employee ID:";
cin>> ID;
}
8
C) Define the public function void putdata() to display the data?
[ 2 marks]
void Employee :: putdata(void)
{
cout<< " Employee name is "<< name;
cout<< "Employee ID is
" << ID;
}
D) Write a main() program to exercise this class. It should create an object of type Employee, and call
functions getdata() and putdata() for this object?
[ 3 marks]
#include<iostream>
int main(void)
{
Employee e1, e2,e3 ;
e1.getdata();
e1.putdata(); cout<<endl;
e2.getdata();
e2.putdata(); cout<<endl;
e3.getdata();
e3.putdata(); cout<<endl;
system("PAUSE");
return 0;
}
9
Download