CS521 – Data Science Principles: Midterm Important Subjective (30 Solved Q&As)
Computer Science • Read every solved subjective question with its model answer directly on this page. Free, no login required.
All 664 Midterm Packs · CS521 Course Page · CS521 MCQ Pack (39 MCQs)
-
Write the CSS code for “Virtual University of Pakistan” (the class .vu-pakistan).
Topic: CSS Basics & Styling Code
.vu-pakistan { background-color: #007bff; color: #ffffff; padding: 20px; border: 1px solid #0069d9; }
-
What are the components of the Application Stack? Name the five layers.
Topic: Application Stack & Web Servers
• Presentation Layer (User Interface) • Application Layer (Business Logic) • Business Layer (Data Processing) • Data Access Layer (Database Interaction) • Infrastructure Layer (Networking and Storage)
-
What is the difference between a Static Server and a Dynamic Server?
Topic: Application Stack & Web Servers
Static Server | Dynamic Server Serves pre-built HTML files | Generates HTML content dynamically Fast and efficient | Supports server-side scripting (e.g. PHP, Python) Limited functionality | More flexible and interactive
-
Write JavaScript code to declare an array of the numbers 1 to 5.
Topic: JavaScript Programming & Arrays
let array = [1, 2, 3, 4, 5];
-
Write a JavaScript function to calculate the sum of an array.
Topic: JavaScript Programming & Arrays
function sumArray(array) { let sum = 0; for (let i = 0; i < array.length; i++) { sum += array[i]; } return sum; } console.log(sumArray([1, 2, 3, 4, 5])); // Output: 15
-
Write a JavaScript program to sum two numbers and print the result.
Topic: JavaScript Programming & Arrays
let num1 = 5; let num2 = 10; let sum = num1 + num2; console.log("The sum is: " + sum); // Output: The sum is: 15
-
Write a JavaScript program using arrays of 5 students and their marks to print each student's marks.
Topic: JavaScript Programming & Arrays
let students = ["Ali", "Sara", "Ahmed", "Zain", "Aisha"]; let marks = [85, 90, 78, 88, 95]; for (let i = 0; i < students.length; i++) { console.log(students[i] + " scored " + marks[i] + " marks."); } Output: Ali scored 85 marks. Sara scored 90 marks. Ahmed scored 78 marks. Zain scored 88 marks. Aisha scored 95 marks.
-
Explain the CSS Box Model with the help of a diagram.
Topic: CSS Box Model
The CSS Box Model defines a structure that wraps around every HTML element. It has 4 main parts: • Content – where the text or images appear • Padding – the space between the content and the border • Border – the edge of the element, which comes after the padding • Margin – the space between the border and the other elements Diagram (from the outermost layer to the innermost): Margin → Border → Padding → Content. (In the source file this answer is written in Roman Urdu; it is given here in English.)
-
Name and describe five CSS selectors and pseudo-classes.
Topic: CSS Selectors & Pseudo-Classes
CSS Selector | Description p {} | Element Selector – styles all <p> elements. .class {} | Class Selector – styles the element(s) having a specific class. #id {} | ID Selector – styles one unique element. :hover {} | Pseudo-Class – the style applies when the user moves the mouse over an element. :first-child {} | Pseudo-Class – selects the first child element of a parent.
-
What is CSS? Write the basic syntax of a CSS rule with an example.
Topic: CSS
CSS (Cascading Style Sheets) is the language used to style HTML documents. It controls the colours, fonts, spacing, borders and layout of web pages. A CSS rule consists of a selector and a declaration block: selector { property: value; } Example: h1 { color: blue; font-size: 24px; } Here h1 is the selector, and color and font-size are properties with their values.
-
What are the three ways to add CSS to an HTML page? Briefly explain each.
Topic: CSS
• Inline CSS – written in the style attribute of an HTML tag, e.g. <p style="color: red;">. It applies to that single element only. • Internal CSS – written inside a <style> tag in the <head> section of the same HTML file. It applies to that page only. • External CSS – written in a separate .css file and linked to the page with <link rel="stylesheet" href="style.css">. One file can style many pages, so this is the most preferred method.
-
Write CSS code to set the text colour of all paragraphs to red and their font size to 16px.
Topic: CSS
p { color: red; font-size: 16px; }
-
Write CSS code to style a button with a green background, white text and 10px padding.
Topic: CSS
button { background-color: green; color: white; padding: 10px; }
-
What is the difference between margin and padding in the CSS Box Model?
Topic: CSS
Padding is the space inside the element, between the content and the border. It increases the element's own size, and the element's background colour shows in the padding area. Margin is the space outside the element, between its border and the neighbouring elements. It is always transparent and does not add to the element's own size.
-
An element has width: 200px, padding: 10px and border: 5px. Calculate the total width the element occupies (excluding margin) according to the standard CSS Box Model.
Topic: CSS
Total width = content width + left and right padding + left and right border = 200 + (2 × 10) + (2 × 5) = 200 + 20 + 10 = 230px
-
What is the difference between a class selector and an ID selector?
Topic: CSS
A class selector (.name) can be applied to many elements on the same page, so it is used when the same style is needed for a group of elements. In HTML it is given as class="name". An ID selector (#name) is applied to only one unique element on a page. In HTML it is given as id="name".
-
Write CSS code that changes the background colour of a link when the user moves the mouse over it.
Topic: CSS
a:hover { background-color: yellow; } :hover is a pseudo-class, so this style is applied only while the mouse pointer is over the link.
-
What does the universal selector (*) do? What is the effect of the rule * { margin: 0; padding: 0; }?
Topic: CSS
The universal selector * selects every element on the page. The rule * { margin: 0; padding: 0; } removes the default margin and padding that browsers apply to elements, so the designer starts with clean, equal spacing on all elements.
-
What is JavaScript? Where does JavaScript code run?
Topic: JavaScript
JavaScript is a programming language used to make web pages interactive and dynamic. It mainly runs inside the web browser (client-side), where it can respond to user actions, change the page content and perform calculations. With Node.js it can also run on the server.
-
Write JavaScript code to declare an array of four fruits, and print its first element and the total number of elements.
Topic: JavaScript
let fruits = ["Apple", "Banana", "Mango", "Orange"]; console.log(fruits[0]); // Output: Apple console.log(fruits.length); // Output: 4
-
Write a JavaScript function that finds and returns the largest number in an array.
Topic: JavaScript
function findMax(array) { let max = array[0]; for (let i = 1; i < array.length; i++) { if (array[i] > max) { max = array[i]; } } return max; } console.log(findMax([85, 90, 78, 88, 95])); // Output: 95
-
Write a JavaScript program to calculate the average of the marks stored in an array.
Topic: JavaScript
let marks = [85, 90, 78, 88, 95]; let sum = 0; for (let i = 0; i < marks.length; i++) { sum += marks[i]; } let average = sum / marks.length; console.log("Average marks: " + average); // Output: Average marks: 87.2
-
Using the students and marks arrays from the source paper — students = ["Ali", "Sara", "Ahmed", "Zain", "Aisha"] and marks = [85, 90, 78, 88, 95] — what will be the output of console.log(students[2]) and console.log(marks[4])?
Topic: JavaScript
Array indexes start from 0, so students[2] is the third element and marks[4] is the fifth element. Ahmed 95
-
Write JavaScript code to add a new element to the end of an array and print the updated array.
Topic: JavaScript
let array = [1, 2, 3, 4, 5]; array.push(6); console.log(array); // Output: [1, 2, 3, 4, 5, 6] The push() method adds an element at the end of the array.
-
Write JavaScript code using a for loop to print the numbers from 1 to 5.
Topic: JavaScript
for (let i = 1; i <= 5; i++) { console.log(i); } Output: 1, 2, 3, 4, 5 (each on a new line).
-
What is the function of the Presentation Layer in the application stack?
Topic: Application Stack & Web Servers
The Presentation Layer is the top layer of the application stack. It is the user interface (UI) — the part of the application that the user sees and interacts with, such as web pages, forms and buttons. It takes input from the user and displays the results returned by the lower layers.
-
Which layer of the application stack is responsible for database interaction? What does it do?
Topic: Application Stack & Web Servers
The Data Access Layer is responsible for database interaction. It handles all communication between the application and the database: it reads data from the database and inserts, updates and deletes records, so the business logic does not have to deal with the database directly.
-
What is the role of the Infrastructure Layer in the application stack?
Topic: Application Stack & Web Servers
The Infrastructure Layer is the bottom layer of the application stack. It provides the networking and storage resources — the servers, network connections and storage — on which the whole application runs.
-
Why are dynamic servers preferred for interactive websites such as login systems and online stores?
Topic: Application Stack & Web Servers
A dynamic server generates HTML content at request time using server-side scripting (e.g. PHP, Python) and can fetch different data for every user from a database. Interactive websites need personalised, changing content — such as a user's own account page or shopping cart — which pre-built static pages cannot provide. That is why dynamic servers are preferred for such websites.
-
State two situations where a static server is a better choice than a dynamic server.
Topic: Application Stack & Web Servers
• For simple websites whose content rarely changes, such as a personal portfolio or an information page — a static server is fast, efficient and cheap to host. • Where simplicity and security matter: a static server runs no server-side scripts and no database, so there is less that can fail or be attacked.