Question
(Mid Term, Marks = 2, Lesson No. )
What happens if the file 'myfile.txt' does not exist and we still try to open it using the function file.open("myfile.txt",ios:out)?
Answer:
Question
(Mid Term, Marks = 2, Lesson No. )
Write down the output of the following code segment?
int x[10] = {0,1,2,3,4,5,6,7,8,9};
int *xptr;
xptr = &x[5];
cout<< *(xptr++) + 1;
Answer:
Question
(Mid Term, Marks = 3, Lesson No. )
Suppose if we want to store values like 10, 45.3, 4, A, D, 6.4, and 50 by using array. Can we store all these elements by declaring a single array? Yes or no? Give reason to support your answer.
Answer:
Question
(Mid Term, Marks = 3, Lesson No. )
Write the code for the declaration of a function named getMax which takes a 2D array of integers(3x3 dimensions) as its parameter and returns a double pointer to an integer.
Answer:
Question
(Mid Term, Marks = 5, Lesson No. )
Write a C/C++ program which defines an array of 15 characters and fills the array with string "12players2teams". Then using character handling functions, counts the number of digits and alphabets in the string and displays on the screen.
Sample output of program:
Number of digits in string: 3
Number of alphabets in string: 12
Answer:
Question
(Mid Term, Marks = 5, Lesson No. )
Write a code segment that displays the diagonal elements of a matrix of NxN.
where N can be any number and represents dimensions of matrix.
Answer:
Question
(Mid Term, Marks = 2, Lesson No. )
Consider the structure
struct Customer
{
int custnum;
int salary;
float commission;
};
Write the code for declaring a Customer type variable cust1 and assign a value 2000 to the structure member salary.
Answer:
Question
(Mid Term, Marks = 2, Lesson No. )
How two dimensional array relates with a matrix?
Answer:
Question
(Mid Term, Marks = 3, Lesson No. )
Identify each of the given functions as string conversion function or string manipulation function.
1. double atof(const char *nptr)
2. char *strcpy(char *s1, const char *s2)
3. int atoi(const char *nptr)
Answer:
Question
(Mid Term, Marks = 3, Lesson No. )
What is the output of following code snippet?
int array[7], sum = 0;
for(int i=0; i<7; i++)
{
array[i] = i;
sum += array[i];
}
cout<<"Sum ="<<sum;
Answer:
Question
(Mid Term, Marks = 5, Lesson No. )
Write the output of following program.
#include<iostream.h>
int main()
{
int arr[5], i = -1, z;
while(i<5)
arr[i] = ++i;
for(i=0; i<5; i++)
cout<<arr[i]<<" ";
return 0;
}
Answer:
Question
(Mid Term, Marks = 5, Lesson No. )
Write a program which takes a string from the user and searches it within another string "This is my Pakistan".
For example, if the user enters the string "my" then after search, the program should display the message "String found" otherwise it should display the message "string not found".
Answer:
Question
(Mid Term, Marks = 2, Lesson No. )
If p and q are two pointers then what is the problem with the following statement?
cout<<"p = "<<", p + q ="<<p + q<<endl;
Answer:
Question
(Mid Term, Marks = 2, Lesson No. )
If a structure is passed to a function, what will be the default calling convention (by value or by reference)?
Answer:
Question
(Mid Term, Marks = 3, Lesson No. )
Explain the logic of the given program code and also determine its output.
#include
#include
main()
{
char name[10] = "Khaqan";
char msg[20] = "Welcome!";
cout<<:strncat(msg, name, 3);
return 0;
}
Answer:
Question
(Mid Term, Marks = 3, Lesson No. )
Write the code for the following task.
Declare an integer array longroutes and a pointer routePtr and then assign the address of array longroutes to the pointer variable routePtr.
Answer:
Question
(Mid Term, Marks = 5, Lesson No. )
Write a program which declares and initializes an integer array of 10 elements and then using a pointer, displays the array elements in reverse order.
Answer:
Question
(Mid Term, Marks = 5, Lesson No. )
Find out errors in the program if any and after correction, write the output.
int main()
{
ofstream outfile;
long position;
char outfilename[] = "abc.txt";
outfile.open(outfilename);
if(!outfile)
{
cout<<"Error Occured while opening a file";
exit(1);
}
outfile.write("This is a car", 12);
position = outfile.tellp();
outfile.seekp(position-5);
outfile.write("jar", 3);
outfile.close();
return 0;
}
Answer:
Question
(Mid Term, Marks = 2, Lesson No. )
A two-dimensional array has 3 rows and 4 columns. Write down the syntax to initialize first element of all three rows of two-dimensional array with value 2.
Answer:
Question
(Mid Term, Marks = 2, Lesson No. )
Identify any logical or syntax error in the statement below and explain the reason of the error.
seekg(-10L, ios::beg);
Answer:
Question
(Mid Term, Marks = 3, Lesson No. )
Using the given array declaration:
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
int* arr_ptr = arr;
Find out the value of each of the expression below.
1. *(arr_ptr + 4)
2. (*arr_ptr)++
3. *(arr_ptr++)
Answer:
Question
(Mid Term, Marks = 3, Lesson No. )
If you are provided two methods for accessing a file; random and sequential. Which method will you prefer and why?
Answer:
Question
(Mid Term, Marks = 5, Lesson No. )
Following are two programming statements; you have to write down the functionality of each statement?
1) int *const intPtr = &x;
2) const int *intPtr = &x;
Answer:
Question
(Mid Term, Marks = 2, Lesson No. )
Write the syntax for calling a string manipulation function which compares the first 5 characters of an array "name" with the string "asdf".
Answer:
Question
(Mid Term, Marks = 2, Lesson No. )
What will be the value of x after the execution of the following code segment?
int x = 10;
int y = 30;
int *xptr = &x;
x = *xptr + 10;
Answer:
Question
(Mid Term, Marks = 3, Lesson No. )
Assume that you write a program to read the data from a text file. You have two options to do it, either reading the data character by character or reading multiple lines of data at a time. Which option do you think is more better and Why?
Answer:
Question
(Mid Term, Marks = 3, Lesson No. )
Consider that an integer pointer xptr contains the memory address 0x22ff50. Now if the statement xptr += 4; is executed, how many bytes the pointer xptr will jump in the memory and what will be the new address of the xptr?
Answer:
Question
(Mid Term, Marks = 5, Lesson No. )
Detect and correct compile time error(s) in the following code.
Hints: The following code print numbers from 1 to 100.
void main()
{
int counter;
float *ptr;
for (counter=1; counter<=100; counter++)
{
ptr = counter;
cout<<ptr<<" ";
getch();
return 0;
}
Answer: