C Plus Plus Miscellaneous - Study Mode
[#711] What will be the output of the following C++ code? #include <stdio.h>
int main ()
{
FILE * p
int c
int n = 0
p = fopen ("myfile.txt", "r")
if (p == NULL)
perror ("Error opening file")
else
{
do {
c = getc (p)
if (c == '$')
n++
} while (c != EOF)
fclose (p)
printf ("%d
", n)
}
return 0
}
Correct Answer
(C) Count of '$' symbol or Error opening file
[#712] What will be the output of the following C++ code? #include <iostream>
#include <algorithm>
#include <vector>
using namespace std
bool myfunction (int i,int j) { return (i<j)
}
int main ()
{
int myints[] = {9, 8, 7, 6, 5}
vector<int> myvector (myints, myints + 5)
partial_sort (myvector.begin(), myvector.begin() + 3, myvector.end())
partial_sort (myvector.begin(), myvector.begin() + 2, myvector.end(),
myfunction)
for (vector<int> :: iterator it = myvector.begin()
it != myvector.end()
++it)
cout << ' ' << *it
return 0
}
Correct Answer
(B) 5 6 7 9 8
[#713] What will be the output of the following C++ code? #include <iostream>
using namespace std
template <class T, int N>
class mysequence
{
T memblock [N]
public:
void setmember (int x, T value)
T getmember (int x)
}
template <class T, int N>
void mysequence<T,N> :: setmember (int x, T value)
{
memblock[x] = value
}
template <class T, int N>
T mysequence<T,N> :: getmember (int x)
{
return memblock[x]
}
int main ()
{
mysequence <int, 5> myints
mysequence <double, 5> myfloats
myints.setmember (0, 100)
myfloats.setmember (3, 3.1416)
cout << myints.getmember(0) << '
'
cout << myfloats.getmember(3) << '
'
return 0
}
Correct Answer
(C) 100 3.1416
[#714] What is the output of this C++ program in the "test.txt" file? #include <fstream>
using namespace std
int main ()
{
long pos
ofstream outfile
outfile.open ("test.txt")
outfile.write ("This is an apple",16)
pos = outfile.tellp()
outfile.seekp (pos - 7)
outfile.write (" sam", 4)
outfile.close()
return 0
}
Correct Answer
(D) This is a sample
[#715] What will be the output of the following C++ code? #include <iostream>
#include <algorithm>
#include <vector>
using namespace std
int main ()
{
int myints[]={ 10, 20, 30, 40, 50 }
vector<int> myvector (4, 99)
iter_swap(myints, myvector.begin())
iter_swap(myints + 3,myvector.begin() + 2)
for (vector<int> :: iterator it = myvector.begin()
it != myvector.end()
++it)
cout << ' ' << *it
return 0
}
Correct Answer
(C) 10 99 40 99