Sei sulla pagina 1di 14

The quiz questions given below were released for the students on CS101.

1X,
IITBombayX.

These are just for your information.

Q1
(2 points possible)
Consider the following expression used in a C++ program
a+b-c*d
If the types of a, b, c, and d are int, long int, float, and double respectively, then the type of the
expression is:
- unanswered
int
long int

float

double

None of the above

You have used 0 of 1 submissions

Q2
(2 points possible)
What will be the output produced by the following program segment?
int varx;
varx = 13 > 10 ? 1 : 0;
cout<<varx;

- unanswered
13
10

You have used 0 of 1 submissions

Q3
(2 points possible)
A Chinese credit card company named Gooda Banka offers some credit points on a certain
amount of purchase made by the customer.

Their scheme is as follows :


If the purchase made is from Rs. 200/- to Rs. 500/-, then they offer 2 credit points on every 100
Rupees spent
If the purchase made is from Rs. 501/- to Rs. 750/-, then they offer 3 credit points on every 90
Rrupees spent
If the purchase made is from Rs. 751/- to Rs. 999/-, then they offer 4 credit points on every 80
Rupees spent
If the purchase made is from Rs. 1,000/- to Rs. 1,500/-, then they offer 6 credit points on every
65 Rupees spent.
Any other purchase amount should be considered INVALID
To attract more customers, they have added one more criterion. i.e. if the customer is associated
with the bank for more than two years, then apart from the credit points increased as per the rules
stateed above, additionally, credit points earned will be increased by 25 points on every purchase
irrespective of the amount.
The following program tries to compute the credit points earned by a customer from Gooda
Banka.
#include <iostream>
using namespace std;
int main () {
float creditPurchase = 0, creditPoints = 0;
int years = 4;
cout <<" Enter the amount spent using credit card " << endl;
cin >> creditPurchase;
if ( creditPurchase < 200 || creditPurchase > 1500)
cout << " Invalid credit purchase " << endl;
else if ( creditPurchase == 200 || creditPurchase <= 500)
creditPoints += (( creditPurchase / 100) * 2);
else if ( creditPurchase == 501 || creditPurchase <= 750 )
creditPoints += (( creditPurchase / 90) * 3);
else if ( creditPurchase == 751 || creditPurchase <= 999)
creditPoints += (( creditPurchase / 80) * 4);
else
creditPoints += (( creditPurchase / 65) * 6);
if ( years >= 2)
creditPoints += 25;
cout << " The credit points earned on purchase of " << creditPurchase << "
is " << creditPoints << endl ;
return 0;

Select from the following which are INVALID. Assume credit purchase is always positive.
- unanswered
The program computes the credit points correctly, if the customer is associated with the bank
for 6 years, and the credit purchase is between 200 and 750

The program computes the credit

points correctly, if the number of years of the customer associated with the bank is greater than
two years
Irrespective of the positive credit purchase amount entered by the user, the
program computes the credit points correctly if the customer is associated with the bank for less
than or equal to two years.
None of these
You have used 0 of 1 submissions

Q4
(2 points possible)
What will be the output produced by the following program segment?
int i,j,sum=10;
for(i=0;i<5;i++)
if(i%2)
for(j=0;j<=3;sum+=j,j++){ }
else
for(j=3;j>0;sum+=j,j--){ }
cout<<sum;

Write the output value as your answer

- unanswered
You have used 0 of 1 submissions

Q5

(2 points possible)
What will be the output produced by the following program segment?
int x=1,i,y;
for(i=0;i<5;i++)
x+=3;
x%=5;
if(x%2==0)
cout<<"x is even";
else
cout<<"x is odd";

- unanswered
x is even

x is odd

None of the choices

You have used 0 of 1 submissions

Q6
(2 points possible)
In the following program, how many times is the function Recursive called, including the call
from the main function? Also, what will be the output of this program?
#include<iostream>
using namespace std;
int Recursive (int n) {
if (n == 0)
return 1;
if (n == 1)
return 2;
return ( Recursive (n - 1) * Recursive (n - 2) );
}
int main () {
int value = Recursive (5);
cout << " Result : " << value << endl ;
return 0;
}

- unanswered
The recursive function will be called 15 times and the following output is produced: Result:
32

The recursive function will be called 14 times and the following output is produced:

Result: 32

The recursive function will be called 13 times and the following output is

produced: Result: 28
is produced: Result: 18

The recursive function will be called 14 times and the following output
None of these

You have used 0 of 1 submissions

Q7
(2 points possible)
What will be the output produced by the following program segment?
int tmp, i, j=0, n=8, y=12;
int A[8]={5,7,12,15,20,2,0,30};
for(i=0;i<n;i++){
if(A[i] < y){
int tmp = A[j];
A[j] = A[i];
A[i] = tmp;
j++;
}
}
int s=0;
for(i=0;i<n;i++){
s=s+A[i];
}
cout<<s/n;

Write the output value as your answer

- unanswered
You have used 0 of 1 submissions

Q8
(2 points possible)
What will be the output produced by the following program segment?

#include<iostream>
using namespace std;
int main() {
int N = 5, i, j, a[11], b[11];
a[0]=3;
b[0]=0;
a[1]=2;
for( i=1; i<N ; i=i+1) {
b[i] = a[i-1] + b[i-1];
if (i >= 2)
a[i] = a[i-1] + b[i-2];
}
i = 0;
while (i < N) {
a[2*(N-i-1)] = a[N-i-1];
a[2*(N-i-1)+1] = b[N-i-1];
i++;
}
for(i=0; i<2*N; i++)
cout << a[i] << " ";
}

return 0;

- unanswered
3 0 2 3 2 3 3 5 7 12

3 0 2 3 2 5 5 7 10 12

3 0 2 3 3 3 3 5 7 12

3 0 2 3 3 2 3 5 10

12
You have used 0 of 1 submissions

Q9
(2 points possible)
Consider the following program. Assume 10 integer values are supplied as input.
#include<iostream>
using namespace std;
int main(){
int n = 10;
int A[10], B[10];
int k = 0;
for(int i=0; i<n; i++)
cin >> A[i];
bool found = false;
for(int i = 0; i<n-1; i++){ //Begin first for loop
found = false;
for(int j=i+1; j<n; j++){ // Begin second for loop
if((A[i]-A[j]) == 0){ // Begin first If

for(int temp=k-1; temp>=0; temp--){// Begin third for loop


if(A[i] == B[temp]){
found = true;
break;
}
} // End of the third for loop
if(found == false){
B[k] = A[i];
k++;
}
} // End of the first if
} // End of the second for loop
} // End of the first loop
return 0;
}

Which of the following can be said about the above program? Select the ones which are
appropriate:
- unanswered
All duplicate elements of array 'A' are stored in array 'B' with as many duplicates as is
present in array 'A'

The program successfully finds all duplicate elements in array 'A' and

stores only one copy of the duplicates in the array 'B'


in array 'A' for some input configuration

The program may not find duplicates

None of these

You have used 0 of 1 submissions

Q10
(2 points possible)
The below given program first finds the length of the main string entered by the user. It then asks
for the substring to be searched in the main string. If the entered substring is found, it displays
the message "substring found at start index 'startindx' and end index 'endindx'. After which, it
asks for the substring to be replaced by the replacement substring, both are to be entered by the
user. Finally, it prints the main string.
#include<iostream>
#include<cstdio>
using namespace std;
//gets the string entered by the user of length 'len'
//It also checks for the maximum number of elements 'maxlength' the string can
contain

//str[] is null-ternminated at the end


void getstring(char str[], int maxlength, int &len){
len=0;
char c;
while((c=getchar())!='\n'){
if(len==maxlength-1){
cout<<"Input exceeded"<<len<<endl;
break;
}
else{
str[len]=c;
len++;
}
}
str[len]='\0';
}
//Identifies the start and the end index of the word present in the main
string
bool findsubstring(char string[], char word[], int l1, int l2, int
&startindex, int &endindex){
bool flag=false; int count, k, i,j;
for(i=0;i<=l1-l2;i++){
count=0;
k=i;
for(j=0;j<l2;j++){
if(string[k]==word[j]) count++;k++;
}
if(count==l2){startindex=i; endindex=i+count-1; flag = true;}
}
return flag;
}
//finds the old string and replaces with the new string in the main string
void findandreplace(char A[], int len1,char oldstring[], char newstring[], int
len2){
for(int i=0;i<len1;i++)
for(int j=0;i<len2;i++)
if(A[i] == oldstring[j]) A[i]= newstring[j];
}
//prints the string on the screen
void printstring(char A[], int len){
for(int i=0;i<len;i++) cout<<A[i];
cout<<endl;
}
int main(){
char c;int n1=1000, n2=100;
char str[n1], word[n2];
int l1,l2;
int startindx, endindx;
bool foundstring=false;
cout<<"Enter the string of length <1000:";
getstring(str,n1,l1);

cout<<"Enter the substring to be searched of length <100:";


getstring(word,n2,l2);
cout<<"l1:"<<l1<<" "<<"l2:"<<l2<<endl;
foundstring=findsubstring(str,word,l1,l2,startindx, endindx);
if(foundstring)
cout<<"substring found at start index "<<startindx<<" and end index
"<<endindx<<endl;
else
cout<<"substring not found";
char oldstr[n2],newstr[n2]; int l3;
cout<<"Enter a substring to be replaced:"<<endl;
getstring(oldstr,n2,l3);
cout<<"Enter a replacement substring:"<<endl;
getstring(newstr,n2,l3);
findandreplace(str,l1,oldstr, newstr, l3);
cout<<"Printing new string"<<endl;
printstring(str,l1);
return 0;
}

Which are the appropriate choices for the above program


- unanswered
The required substring cannot be found, but, it will find the substring, if the statement
"if(count==l2){startindex=i; endindex=i+count-1; flag = true;}" in the above findsubstring
function is replaced by "if(count==l2){startindex=i; endindex=j; flag = true;}"
The
findandreplace function does not do its job properly

The function getstring does not get the

proper length of the string and hence leads to problems in searching substring

The start index

and the end index calculated for substring search is not correct
You have used 0 of 1 submissions

Q11
(2 points possible)
Consider the following program that prints the sequence of characters based on the value
of variable 'counter'
#include<iostream>
using namespace std;
int myFunction(char *name, int counter) {
if(counter == 1)

return 0;
else {
counter--;
if(counter == 4) {
for(int i=0;i<counter;i++)
cout<<name[i];
cout<<endl;
}
myFunction(name,counter);
if(counter == 4) {
for(int i=0;i<=counter;i++)
cout<<name[i];
cout<<endl;
}
return 0;
}
}
int main(){
int intNum=9;
char name[]={'C','S','1','0','1','.','2','x'};
myFunction(name, intNum);
return 0;
}

The value 9 is passed from the main function to the variable 'counter'. What will the function
'myFunction' print.
- unanswered
CS10
CS101
CS10
CS10
CS101
CS10
CS101
CS101

None of these
You have used 0 of 1 submissions

Q12
(2 points possible)

Consider the following program


#include<iostream>
using namespace std;
int finalResult(int *intResult1, int intResult2)
int *intTemp1 = &intResult2;
int **intTemp2 = &intTemp1;
*intTemp1 = *intTemp1+1;
return *intResult1 - EXP;
}
int main() {
int intNum1=14, intNum2=7;
cout<<finalResult(&intNum1,intNum2)<<endl;
return 0;
}

Select the appropriate choices


- unanswered
The function 'finalResult' will return value 7 to the main function, if we replace EXP with
*(&intResult2).

The function 'finalResult' will return value 7 to the main function, if we

replace EXP with **intTemp2

The function 'finalResult' will return value 6 to the main

function, if we replace EXP with **intTemp2

None of these

You have used 0 of 1 submissions

Q13
(2 points possible)
What is the result of execution of the following C++ program if the user enters
thequickbrownfox when prompted to enter myString ?
#include<iostream>
using namespace std;
int main ()
{
char myString[12];
cout << "Enter the string : ";
cin >> myString ;
cout << myString ;
return 0;
}

Select from the folowing which are VALID:


- unanswered
The program gives compilation error.

The program will compile successfully and print

the string thequickbrownfox without any error messages


the string thequickbr without any error messages
will likely crash

The program will compile and print

The program will compile successfully but

None of these

You have used 0 of 1 submissions

Q14
(2 points possible)
Consider the following piece of code which removes alternate characters from a file 'file_path1'
and writes the result to a new file 'file_path2'. The first character is copied, the second is skipped,
and so on. Assume that 'file_path1' and 'file_path2' are defined earlier and have paths to valid
files on the disk.
#include<iostream>
#include<cstdio>
using namespace std;
int main(){
const char* file_path1="input.txt";
const char* file_path2="output.txt";
FILE *fpt = NULL, *fpt_dest = NULL;
fpt = fopen(file_path1, "r");
if(!fpt) return -1;
fpt_dest = fopen(file_path2, "w");
if(!fpt_dest) return -1;
//INSERT THE SNIPPET CODE HERE

fclose(fpt);
fclose(fpt_dest);

Which of the following snippet of code if replaced with "INSERT THE SNIPPET CODE
HERE", will execute the program correctly as mentioned in the question?

- unanswered
// Snippet Code 1:
while(true){
char tmp;
fscanf(fpt, "%c", &tmp);
if(feof(fpt))
break;
fprintf(fpt_dest, "%c", tmp);
fscanf(fpt, "%c", &tmp);
if(feof(fpt))
break;
}

// Snippet Code 2:
bool flag1 = true;
bool flag2 = true;
while(flag1){
char tmp;
fscanf(fpt,"%c", &tmp);
if(feof(fpt))
flag1 = false;
if(flag2)
fprintf(fpt_dest, "%c", tmp);
flag2 = !flag2;
}

// Snippet Code 3:
while(1){
char tmp;
fscanf(fpt,"%c", &tmp);
if(!feof(fpt))
fprintf(fpt_dest, "%c", tmp);
fscanf(fpt,"%c", &tmp);
}

// Snippet Code 4:
while(1){
char tmp1, tmp2;
fscanf(fpt, "%c", &tmp1);
fscanf(fpt, "%c", &tmp2);
fprintf(fpt_dest, "%c", tmp1);
fprintf(fpt_dest, "%c", tmp2);
}

None of these

You have used 0 of 1 submissions

Q15
(2 points possible)
Consider the following 'main' program:
#include <iostream>
#include<cstdio>
using namespace std;
int main(){
FILE *fp;
int d= 0;
fp = fopen("file.txt", "r");
if (fp == NULL)
cout<<"File not found";
else{
fseek(fp, 0, 2);
d = ftell(fp);
fclose(fp);
}
return 0;
}

Out of the options given below, which of the option is correct:


- unanswered
The above program will copy the value of first location of file 'file.txt', in the variable 'd'
The above program will get the size of a file 'file.txt', and copy it in the variable 'd'.
above program will not compile
You have used 0 of 1 submissions

None of the above

The

Potrebbero piacerti anche