0% found this document useful (0 votes)
21 views4 pages

Woldia University Computing Exam 2005

Uploaded by

tegegne ayalew
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views4 pages

Woldia University Computing Exam 2005

Uploaded by

tegegne ayalew
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Woldia University

Faculty of Technology
Department of Electrical and Computer Engineering
Introduction to Computing final examination for 2nd year 1st
semester students A.Y: 2005 E.C
Time allowed: 3:00 hrs.
Max. Mark: 60%
I. Write “True” if the statement is correct or “False” if the statement is incorrect.(2 Mark
each)
1. A structure is a collection of Variables of different data types.
2. When we declare an array, its name is a const pointer to the first element of the array.
3. Division arithmetical operation is allowed to be conducted on pointers.
II. Multiple choices(2 Mark each)
4. A ‘function calling statement’ must supply arguments that
A. match the function header in number
B. match the function header in number and order
C. match the function header in number, order, and data type
D. match the function header in number, names, and data type
5. When accessing a structure member, the identifier after the dot operator is the name of

A. a structure member. C. a structure variable.


B. a structure tag. D. the keyword struct.

6. Overloaded functions are:


A. same named functions.
B. functions having the same return type.
C. functions performing the same process.
D. functions having the same parameter.
7. An array:
A. is a collection of elements belonging to the same data type.
B. can have only one dimension
C. is a collection of similar variable with different data type
D. is a collection of the same variable
8. The number of bytes occupied by the two dimensional array int A[10][10] are:

A. 100 bytes C. 20 bytes


B. 10 bytes D. 40 bytes

9. A pointer stores:

A. an address of a variable C. a data type of an address variable


B. the values of the variable it points to D. a set of unallocated bytes of memory
10. The expression *num:
A. is a pointer to the variable num

1
B. holds the address of variable num
C. refers to the contents of the num.
D. refers to the contents stored to the variable to which post points to.

III. Short Answer(3 mark each)


11. Write the output of the following piece of code:
# include <iostream.h>
int main()
{
int n=8,i=1,p;
while ( i<=10)
{
p = i+n;
cout<<n<<“+”<<i<<“=”<<p<<endl;
i = i +2;
}
return 0;
}
12. Consider the given declarations and give the outputs.
# include <iostream.h>
int main()
{
int t=90;
int* u = &t;
t-= 10;
(*u)++;
t=t+20;
cout<<“*u=”<<*u<<endl;
return 0;
}
13. Determine the output of the following piece of code:
# include<iostream.h>
void function1(int &a, int &b)
{
a++;
b = b + 5;
}
int main()
{
int m=7, n=2;
function1 (m, n);
cout<<"m= "<<m;

2
cout<<”n=”<<n;
return 0;
}

3
14. Consider the following character array:
char name[] = “ I like studying C++ ”;
What output do the following statements produce?
a. cout<<*name; // _______
b. cout<<*(name+7); //_______
c. cout<<*(name+12); //_______
15. What do the following codes do with two strings with names S1 and S2?
a. strcpy (S1, S2);
b. strcmp(S1, S2);
c. strcat (S1, S2);
IV. Program
16. Write a C++ program that will print the following shape.(5 marks)
*
***
*****
*******
*********
*
***
*****
*******
*********
17. Write a program which asks the user to enter one or more sentences and counts the
numbers of ‘a’ in the sentence. (4 marks)
18. Write a program that accepts five integer values from the key board and print the average
of these numbers. (4 marks)
19. write a program that finds
a. The sum of two N x M matrices. (3 marks)
b. The difference of two N x M matrices ( 3 marks)
20. Write C++ statements to accomplish the following: ( 6 marks)
a. Write structure definition for structure ‘student’ having integer variable ‘IDNo’,
character array ‘studentName’ with names as long as 20 [Link] a
structure variable ‘Stud1‟ for structure ‘student’.
b. Initialize the structure member ‘studentName’ to ‘Dawit’.
c. Initialize the structure member ‘IDNo’ to ‘1872’.
d. Check whether the student name is Abrham if so, the program should print a suitable
message on the screen.

With best wishes!!!


4

Common questions

Powered by AI

Pointer arithmetic is important in C++ because it allows direct manipulation of memory addresses, thus enabling efficient operations like iteration over array elements or data structure traversal. However, its use is limited by type safety; operations such as division on pointers are disallowed as they do not make sense in a memory context. Pointer arithmetic is strictly constrained to addition or subtraction of scalars, which aligns with array indexing and memory block traversal, ensuring operations remain within the bounds of defined memory areas .

The dereferencing operator (*) in C++ is used to access the value stored at the memory location to which a pointer is pointing. When you use the * operator on a pointer, you retrieve the content stored at that specific address. This allows modification and access of the data directly, enabling manipulations and operations on the actual data rather than just the address, thus allowing pointers to effectively point to variables and array elements in memory .

When a pointer is initialized with the address of a variable and the variable is later modified through either the pointer or directly, the same change reflects across both due to the shared memory address. For example, if a variable t is decremented by 10 and then incremented through a pointer by one, modifications reflect in its actual memory location, ensuring any reference or dereference to t or its pointer, reflects the updated value .

When defining a structure in C++, such as 'student', it is crucial to ensure that each field, like ID and Name, is appropriately typed to represent the data effectively. ID can be an integer to handle numeric identifiers, while Name should be a character array with enough space to store the intended length, and operations like initialization and modification are straightforward. Proper initialization is vital for memory setup, and conditional checks (e.g., validating field values) are important for maintaining data integrity and executing conditional logic based on structure content, such as checking for a specific name .

The code generates an arithmetic sequence where it increases the variable i by 2 in each iteration starting from 1 and going up to 9. This results in printing the sums of 8 and each of these odd numbers on separate lines: '8+1=9', '8+3=11', '8+5=13', '8+7=15', and '8+9=17'. This sequence is printed because the loop condition checks for i to be less than or equal to 10 .

Matrix operations such as sum and difference can be effectively implemented in C++ using nested loops to iterate over the rows and columns. For the sum, iterate through each element of two matrices of size N x M, adding corresponding elements from each matrix and storing in a result matrix. Similarly, for difference, subtract corresponding elements. This requires careful index management to ensure operations are contained within the matrix bounds, usually implemented with two loops: one iterating over rows, and an inner loop iterating over columns .

Declaring an array's name as a const pointer to the first element of the array implies that while you can use the array name to access the elements of the array, you cannot change the address it points to. The name itself represents a fixed address in memory where the first element of the array is stored, and manipulating it would lead to invalid operations or errors. This is crucial because it enforces the static nature of arrays and maintains the integrity of memory usage dedicated to array storage .

Function overloading enhances the usability of functions in C++ by allowing multiple functions to have the same name but differ in parameter types or numbers. This means a single operation can be performed using different types of data, improving code clarity and reusability. Instead of creating numerous function names for similar operations, overloading allows the use of one identifier, simplifying function management and improving code readability .

The first statement "cout<<*name;" outputs 'I' because it points to the first character of the array. The second statement "cout<<*(name+7);" outputs 's', since it accesses the character including space offset by 7 which lands on the 's' of 'studying'. The third statement "cout<<*(name+12);" outputs 'u', referring to 12 positions after the beginning, which falls on the 'u' in 'studying' .

Overloaded functions and templates both promote code reuse but in different ways. Overloaded functions have the same name with different parameter lists, requiring a separate function for each data type. In contrast, templates enable a single function definition to work with any data type via a mechanism where the compiler generates a version for each data type used, making templates more versatile when dealing with operations that apply across multiple types without needing specific declarations for each type. Templates thus reduce coding redundancy more effectively but are also more complex to implement .

You might also like