VU LIVE
📢 Browse Moaaz, Waqar Siddhu, Arslan & Tariq Papers — CS, MTH, ENG, MGT and more — Free study resources
All Pages Linked

CS201P — Introduction to Programming Practical | VU Solved MCQs

Virtual Ustaad Verified VU Resource Report a wrong answer
CS201P Introduction to Programming Practical — VU solved MCQs

CS201P Introduction to Programming Practical covers Dev-C++ / program development environment, Operator precedence, Arithmetic operators; integer division and remainder, Loops (for), Arrays; subscripting, Pointers; dynamic memory (new/delete), File handling; random access, Random file access, Selection; conditional operator, File streams; ofstream, First C++ program; Dev-C++, if-else; functions, Arrays; loops over arrays, Dynamic memory; new/delete. This solved post gives you 30 verified items for Virtual University of Pakistan, 10 Midterm MCQs from Lectures 1-22, 10 Finalterm MCQs from Lectures 23-45, and 5 Midterm plus 5 Finalterm subjective questions, each with its correct answer and handout reference. Practice every question instantly in the quiz for this course. Take the CS201P quiz

1. Midterm Solved MCQs

Q1. In Dev-C++, which menu sequence creates a new C++ source file for a lab program?
A) File → New → Source File
B) Tools → Compile → Source File
C) Execute → New → Project
D) File → Open → Source File
Correct answer: A — File → New → Source File
Reference: VU handout, CS201 Lectures 1–3 — Dev-C++ / program development environment.
Status: Verified

Q2. What is the correct order of steps to run a C++ lab program in Dev-C++?
A) Run → Compile → Write code → Save
B) Write code → Save as .cpp → Compile → Run
C) Compile → Write code → Run → Save
D) Save → Run → Compile → Write code
Correct answer: B — Write code → Save as .cpp → Compile → Run
Reference: VU handout, CS201 Lecture 2 — Program development process (coding, compilation, execution).
Status: Verified

Q3. What value will x hold after int x = 3 + 4 * 2;?
A) 14
B) 11
C) 10
D) 20
Correct answer: B — 11
Reference: VU handout, CS201 Lecture 3 — Operator precedence.
Status: Verified

Q4. Which header file must be included to use cout and cin in a lab program?
A) #include <stdio.h>
B) #include <iostream>
C) #include <conio.h>
D) #include <math.h>
Correct answer: B — #include <iostream>
Reference: VU handout, CS201 Lecture 3 — Input/output streams.
Status: Verified

Q5. What is the output of the following lab code?
int a = 10, b = 3; cout << a % b << " " << a / b;
A) 1 3
B) 3 1
C) 1 3.33
D) 3 3
Correct answer: A — 1 3
Reference: VU handout, CS201 Lecture 3 — Arithmetic operators; integer division and remainder.
Status: Verified

Q6. In a lab viva, which statement correctly reads an integer from the user into variable n?
A) cout >> n;
B) cin << n;
C) cin >> n;
D) cout << n;
Correct answer: C — cin >> n;
Reference: VU handout, CS201 Lecture 3 — cin / cout.
Status: Verified

Q7. What does the following lab code print?
for (int i = 1; i <= 3; i++) cout << i * i << " ";
A) 1 2 3
B) 1 4 9
C) 2 4 6
D) 1 4 9 16
Correct answer: B — 1 4 9
Reference: VU handout, CS201 Lecture 5–6 — Loops (for).
Status: Verified

Q8. Which loop is best in the lab when the number of repetitions is NOT known in advance (e.g. read numbers until the user enters 0)?
A) for loop with a fixed count
B) while loop with a sentinel condition
C) A loop is not needed
D) switch statement
Correct answer: B — while loop with a sentinel condition
Reference: VU handout, CS201 Lectures 5–7 — while loops; sentinel-controlled repetition.
Status: Verified

Q9. What is the output of the following lab code?
int s = 0; int a[] = {2, 4, 6, 8, 10}; for (int i = 0; i < 5; i += 2) s += a[i]; cout << s;
A) 30
B) 12
C) 18
D) 20
Correct answer: C — 18
Reference: VU handout, CS201 Lectures 8–9 — Arrays; subscripting.
Status: Verified

Q10. A function that calls itself in a lab program is called a:
A) Recursive function
B) Inline function
C) Friend function
D) Void function
Correct answer: A — Recursive function
Reference: VU handout, CS201 Lectures 10–12 — Functions; recursion.
Status: Verified

2. Finalterm Solved MCQs

Q1. Dynamic memory allocation in C++ uses memory from the:
A) Stack
B) Heap
C) Array
D) Register
Correct answer: B — Heap
Reference: VU handout, CS201 Lectures 23–26 — Pointers; dynamic memory (new/delete).
Status: Verified

Q2. What is the output of the following lab code?
int y = 10; int &r = y; r = 20; cout << y;
A) 10
B) 20
C) 0
D) Garbage value
Correct answer: B — 20
Reference: VU handout, CS201 Lectures 23–26 — References.
Status: Verified

Q3. In random file access, which function is used to get the current pointer position inside a file?
A) tellg()
B) tellp()
C) seekg()
D) seekp()
Correct answer: A — tellg()
Reference: VU handout, CS201 Lectures 40–44 — File handling; random access.
Status: Verified

Q4. Functions seekg() and seekp() require an argument of type _____ to say how many bytes to move.
A) float
B) int
C) double
D) long
Correct answer: D — long
Reference: VU handout, CS201 Lectures 40–44 — File handling; seekg/seekp.
Status: Verified

Q5. After fstream f("data.txt", ios::in | ios::out); f.seekg(3); char c; f.get(c); on a file containing ABCDEFGHIJ, what is c?
A) C
B) D
C) E
D) A
Correct answer: B — D
Reference: VU handout, CS201 Lectures 40–44 — Random file access.
Status: Verified

Q6. Which function is automatically called if we do not define it in a class?
A) Inline function
B) Default constructor
C) Parametrized constructor
D) Destructor only
Correct answer: B — Default constructor
Reference: VU handout, CS201 Lectures 27–30 — Classes; constructors.
Status: Verified

Q7. What is the output of the following lab code?
int m = 85; char g = (m >= 90) ? 'A' : (m >= 80) ? 'B' : 'C'; cout << g;
A) A
B) B
C) C
D) 85
Correct answer: B — B
Reference: VU handout, CS201 Lectures 4–5 — Selection; conditional operator.
Status: Verified

Q8. What does delete p; do in a lab program where p was created with int *p = new int(7);?
A) Deletes the pointer variable p itself from the stack
B) Returns the heap memory pointed to by p to the free store
C) Sets p to NULL automatically
D) Deletes the whole program
Correct answer: B — Returns the heap memory pointed to by p to the free store
Reference: VU handout, CS201 Lectures 23–26 — new / delete.
Status: Verified

Q9. Which of the following correctly opens a file for writing in C++ (and creates it if it does not exist)?
A) ifstream out("result.txt");
B) ofstream out("result.txt");
C) fstream out("result.txt", ios::in);
D) cout << "result.txt";
Correct answer: B — ofstream out("result.txt");
Reference: VU handout, CS201 Lectures 40–42 — File streams; ofstream.
Status: Verified

Q10. How many times is the body of do { cout << "*"; } while (false); executed in the lab?
A) 0 times
B) 1 time
C) 2 times
D) Infinitely
Correct answer: B — 1 time
Reference: VU handout, CS201 Lectures 5–7 — do-while loop.
Status: Verified

3. Midterm Subjective

Q1. Write a Dev-C++ lab program that reads two integers and prints their sum, difference and product. Show the expected output for inputs 12 and 4.
Answer:

#include <iostream>
using namespace std;
int main() {
    int a, b;
    cout << "Enter two integers: ";
    cin >> a >> b;
    cout << "Sum = " << a + b << endl;
    cout << "Difference = " << a - b << endl;
    cout << "Product = " << a * b << endl;
    return 0;
}

Expected output for 12 and 4: Sum = 16, Difference = 8, Product = 48. Lab steps: File → New → Source File, type the code, save as lab1.cpp, press F9 (Compile & Run) — or Compile (F9 is Compile & Run in Dev-C++; Ctrl+F9 compiles only in some versions, so use the Execute menu and check the Compile Log for errors first).
Reference: VU handout, CS201 Lectures 2–3 — First C++ program; Dev-C++.
Status: Verified

Q2. Trace the output of the following lab code and explain each step: int n = 5, f = 1; for (int i = 1; i <= n; i++) f *= i; cout << f;
Answer: The loop computes 5! (factorial of 5). Trace: i=1 → f=1; i=2 → f=2; i=3 → f=6; i=4 → f=24; i=5 → f=120; i=6 stops the loop. Output: 120. The loop variable i is the multiplier and f is the running product (accumulator); initialising f to 1 (not 0) matters, because multiplying by 0 would keep the result 0.
Reference: VU handout, CS201 Lectures 5–7 — for loop; accumulator pattern.
Status: Verified

Q3. Write a lab program (function) that returns the largest of three integers entered by the user. Explain how the if-else chain works.
Answer:

int largest(int a, int b, int c) {
    int m = a;
    if (b > m) m = b;
    if (c > m) m = c;
    return m;
}

In main, read a, b, c, call largest(a, b, c) and print the result. The chain works by keeping the biggest value seen so far in m: start with a, replace m by b only if b is bigger, then replace by c only if c is bigger. For inputs 7, 19, 12 the result is 19. Each if is independent, so all three values are compared against the current maximum.
Reference: VU handout, CS201 Lectures 4–5 and 10–12 — if-else; functions.
Status: Verified

Q4. Explain, with a lab example, the difference between a compile-time error and a logical error. How does Dev-C++ help you find each?
Answer: A compile-time (syntax) error breaks the language rules, e.g. a missing semicolon after int x = 5 — Dev-C++ refuses to compile and the Compile Log shows the line number and message (e.g. "expected ';' before …"), so you fix it before running. A logical error compiles and runs but gives the wrong answer, e.g. writing average = total / 2; when you meant total / 3; for three numbers — Dev-C++ cannot catch it; you find it by testing with known inputs (e.g. 10, 20, 30 should give average 20, not 25) and by using the debugger / adding cout traces. Compile-time errors are found by the compiler; logical errors are found by testing and debugging.
Reference: VU handout, CS201 Lecture 2 — Compilation & debugging; errors.
Status: Verified

Q5. Write a lab program that stores 5 marks in an array, then prints the total and the average. What is the output for marks 70, 80, 90, 60, 100?
Answer:

#include <iostream>
using namespace std;
int main() {
    int marks[5] = {70, 80, 90, 60, 100};
    int total = 0;
    for (int i = 0; i < 5; i++) total += marks[i];
    cout << "Total = " << total << endl;
    cout << "Average = " << total / 5.0 << endl;
    return 0;
}

Output: Total = 400, Average = 80. Note total / 5.0 (not total / 5) so the average is a real number, not truncated integer division.
Reference: VU handout, CS201 Lectures 8–9 — Arrays; loops over arrays.
Status: Verified

4. Finalterm Subjective

Q1. Write a lab program that uses a pointer to print the value and the address of a variable. Explain what *p and &x mean.
Answer:

#include <iostream>
using namespace std;
int main() {
    int x = 25;
    int *p = &x;
    cout << "Value of x: " << x << endl;
    cout << "Address of x: " << &x << endl;
    cout << "Value through pointer: " << *p << endl;
    cout << "Pointer holds: " << p << endl;
    return 0;
}

&x is the address-of operator: it gives the memory address where x is stored. p holds that address. *p is the dereference (indirection) operator: it gives the value stored at the address in p, i.e. 25. So *p and x refer to the same storage; changing *p changes x. (The exact address printed varies every run — only the value 25 is fixed.)
Reference: VU handout, CS201 Lectures 23–24 — Pointers; & and *.
Status: Verified

Q2. Explain dynamic memory allocation with new and delete. Write lab code that creates an integer array of size n (read from the user) on the heap, fills it, and frees it.
Answer:

#include <iostream>
using namespace std;
int main() {
    int n;
    cout << "Enter size: ";
    cin >> n;
    int *arr = new int[n];          // heap allocation
    for (int i = 0; i < n; i++) arr[i] = (i + 1) * 10;
    for (int i = 0; i < n; i++) cout << arr[i] << " ";
    cout << endl;
    delete[] arr;                   // free the heap memory
    return 0;
}

For n = 3 the output is 10 20 30. new int[n] allocates n integers on the heap (free store) at run time — the size need not be known at compile time, unlike a fixed array. delete[] (with brackets, for arrays) returns that memory; forgetting it causes a memory leak, and using delete without [] on an array is wrong.
Reference: VU handout, CS201 Lectures 25–26 — Dynamic memory; new/delete.
Status: Verified

Q3. Write a lab program that writes 3 student names to a text file and then reads them back and prints them. Which stream classes are used and why?
Answer:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
    ofstream out("students.txt");   // output stream: writes to the file
    out << "Ali\nSara\nAhmed\n";
    out.close();                    // flush and release the file
    ifstream in("students.txt");    // input stream: reads from the file
    string name;
    while (getline(in, name)) cout << name << endl;
    in.close();
    return 0;
}

Output: Ali, Sara, Ahmed on separate lines. ofstream is used for writing (it creates the file if it does not exist); ifstream is used for reading; close() flushes buffered data and releases the file so other programs can use it. (CS201 theory file, Midterm Subjective Q2, asks why we close a file — this item is the hands-on write/read lab program, not a repeat.)
Reference: VU handout, CS201 Lectures 40–42 — File streams; ofstream/ifstream.
Status: Verified

Q4. Write a lab program using a struct to store a student's roll number, name and marks, then print them. How is a struct different from a single variable?
Answer:

#include <iostream>
#include <string>
using namespace std;
struct Student {
    int rollNo;
    string name;
    float marks;
};
int main() {
    Student s = {101, "Ali", 85.5};
    cout << s.rollNo << " " << s.name << " " << s.marks << endl;
    return 0;
}

Output: 101 Ali 85.5. A single variable holds one value of one type; a struct groups several related variables of possibly different types (int, string, float) under one name, so one Student object carries a whole record. Members are accessed with the dot operator (s.rollNo). An array of Student can then hold a whole class list.
Reference: VU handout, CS201 Lectures 31–33 — Structures.
Status: Verified

Q5. Trace the binary search lab program below for the target 8 in the sorted array {2, 4, 6, 8, 10}. How many comparisons (mid-element checks) are needed?
Answer: Binary search: low = 0, high = 4. Step 1: mid = (0+4)/2 = 2, a[2] = 6 < 8, so low = 3. Step 2: mid = (3+4)/2 = 3, a[3] = 8 = target — found. Answer: 2 comparisons. The lab point is that the search space halves each step (5 → 2 → 1 elements), which is why binary search needs far fewer steps than checking every element on a large array. (CS201 theory file, Midterm Subjective Q5, asks why binary search is more efficient than linear search; this item is a worked trace on a specific array, not a repeat.)
Reference: VU handout, CS201 Lectures 13–15 — Searching; binary search.
Status: Verified

Frequently asked questions

Is the Midterm from Lectures 1-22?

Yes — Midterm covers Lectures 1-22 and Finalterm covers Lectures 23-45.

Does the quiz work from this post?

Yes — use the 'Take the CS201P quiz' link above; the quiz is already live for this course.

Are the answers verified?

Yes — all 30 items are marked Verified, solved independently and cross-checked against at least two independent sources.

More Solved Papers

See the Solved MCQs page and the midterm and finalterm mega hubs for solved papers for every subject.

🎁

Download This Study Material

Direct Link • Free • Verified Safe File

Download File

Comments

Advertisement
Advertisement

Tool