University of Central Punjab
Faculty of Information Technology
Program BSCS Semester Fall- 2024
Course Title Programming Fundamental Course Code CP113
Course
Amna Mahmood Section B5
Instructor
Time Allowed 15 Min Total Marks 10
Student Name Registration No.
Quiz 1 A
CLO Taxonomy
NO CLO STATEMENT PLO Level
1 Be able to identify the logical and/or syntax errors in a C1
program. 1
[Link] from the given options.
1. How do you close a file in C++?
a. [Link]()
b. closeFile()
c. fclose()
d. endFile()
2. What is the correct syntax for writing data to a file in C++?
a. [Link](data)
b. write(file, data)
c. file << data
d. data << file
3. Which of the following is used to create a stream that performs both input and
output operations?
a. ofstream
b. ifstream
c. iostream
d. fstream
4. Which of the following statements are correct?
a. It is not possible to combine two or more file opening mode in open() method.
[Link] is possible to combine two or more file opening mode in open() method.
c. ios::in and ios::out are input and output file opening mode respectively.
5. What will be the output of following code.
#include <iostream>
#include <fstream>
using namespace std;
int main () {
ifstream infile("[Link]");
ofstream outfile("[Link]");
string line;
while (getline(infile, line)) {
outfile << line << endl;
}
[Link]();
[Link]();
return 0;
}
a. Copies content of [Link] to [Link]
b. Runtime error
c. Deletes content of [Link]
d. Compilation error
6. What is the error in this code?
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ofstream file("[Link]", ios::in);
file << "Hello, World!";
[Link]();
return 0;
}
a. It will compile successfully and write to the file.
b. It will raise a compilation error.
c. It will not write anything to "[Link]".
d. It will raise a runtime error.
7. What will happen if you run this code?
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ifstream file("[Link]");
file << "Trying to write to read-only file";
[Link]();
return 0;
}
a. It will write to "[Link]".
b. It will raise a compilation error.
c. It will compile but will not write anything.
d. It will create the file "[Link]".
8. What is wrong with this code?
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ofstream file("[Link]", ios::out);
[Link]();
file << "Writing after closing the file";
return 0;
}
a. It will write "Writing after closing the file" to "[Link]".
b. It will display a runtime error.
c. It will not write anything to "[Link]".
d. It will reopen "[Link]" and write to it.
9. What will this code print?
#include <fstream>
#include <iostream>
using namespace std;
int main() {
ifstream file("[Link]", ios::in);
char ch;
[Link](ch);
cout << ch;
[Link]();
return 0;
}
a. It will print an empty character.
b. It will print garbage data.
c. It will throw a runtime error.
d. It will print single character.
10. What will this code print?
#include <fstream>
#include <iostream>
using namespace std;
int main() {
char buffer[10];
ifstream file("[Link]");
if (file.is_open()) {
[Link](buffer, 10);
cout << buffer;
[Link]();
} else {
cout << "File could not be opened!";
}
return 0;
}
Assume [Link] contains the text: "Hello, World!" (13 characters).
a. Hello, Wo
b. Hello, Worl
c. Hello, Worl followed by random characters
d. Compilation error