0% found this document useful (0 votes)
5 views10 pages

MCQ C HTML CSS JS

The document contains a multiple-choice question (MCQ) format quiz covering topics in C/C++, HTML, CSS, and JavaScript, consisting of 50 questions with options, answers, and explanations. The questions test knowledge on various programming concepts, syntax, and best practices in these languages. Each section provides detailed explanations for the correct answers to enhance understanding.

Uploaded by

2023322256.mohd
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views10 pages

MCQ C HTML CSS JS

The document contains a multiple-choice question (MCQ) format quiz covering topics in C/C++, HTML, CSS, and JavaScript, consisting of 50 questions with options, answers, and explanations. The questions test knowledge on various programming concepts, syntax, and best practices in these languages. Each section provides detailed explanations for the correct answers to enhance understanding.

Uploaded by

2023322256.mohd
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

MCQ

C / C++ •Answer Sheet


HTML • CSS • JavaScript
50 Questions with Options, Answers & Explanations

Section 1: C / C++ (10 Questions)

1. What is the output of: int x = 5; cout << ++x;


A) 5
B) 6
C) 7
D) Compilation Error
Answer: B) 6

Explanation: ++x is pre-increment — x is incremented to 6 before being passed to cout, so 6 is printed.

2. What is the difference between malloc() and new in C++?


A) malloc() calls constructors; new does not
B) new calls constructors; malloc() does not
C) Both are identical in behaviour
D) malloc() is only for arrays
Answer: B) new calls constructors; malloc() does not

Explanation: new allocates memory AND calls the object's constructor. malloc() only allocates raw memory
without initialisation. Also, new throws std::bad_alloc on failure while malloc() returns NULL.

3. What is a virtual function in C++?


A) A function defined outside a class
B) A function that cannot be overridden
C) A function that supports runtime polymorphism via dynamic dispatch
D) A function with no return type
Answer: C) A function that supports runtime polymorphism via dynamic dispatch

Explanation: A virtual function declared with the 'virtual' keyword enables the correct overridden version to be
called at runtime through a base-class pointer or reference.

4. What is the default return type of main() in C++?


A) void
B) int
C) float
D) char
Answer: B) int

Explanation: The C++ standard mandates that main() returns int. A return value of 0 indicates successful
execution to the operating system.

5. Which header file is required for printf() in C?


A) <stdlib.h>
B) <string.h>
C) <stdio.h>
D) <conio.h>
Answer: C) <stdio.h>

Explanation: printf() is declared in <stdio.h> (Standard Input/Output header). In C++, the equivalent is <cstdio>.

6. What does the static keyword mean inside a function?


A) The variable is globally accessible
B) The variable retains its value between function calls
C) The variable is read-only
D) The variable is allocated on the heap
Answer: B) The variable retains its value between function calls

Explanation: A static local variable is initialised only once and persists for the entire program lifetime, retaining its
last value across subsequent function calls.

7. What is the use of the const keyword?


A) Declares a constant pointer only
B) Prevents a variable or object from being modified after initialisation
C) Makes a function inline
D) Allocates memory statically
Answer: B) Prevents a variable or object from being modified after initialisation

Explanation: const enforces immutability. It can be applied to variables, function parameters, member functions
(const methods), and pointers to restrict modification.

8. Which operator is overloaded for object copying?


A) = (Assignment operator)
B) == (Equality operator)
C) & (Address-of operator)
D) -> (Arrow operator)
Answer: A) = (Assignment operator)

Explanation: The copy assignment operator (operator=) is overloaded to define how one object is copied to
another. The copy constructor handles initialisation from another object.

9. What is a segmentation fault?


A) A syntax error at compile time
B) A runtime error caused by illegal memory access
C) An error caused by dividing by zero
D) A linker error
Answer: B) A runtime error caused by illegal memory access

Explanation: A segfault occurs when a program accesses memory it is not permitted to access — common
causes include dereferencing NULL/dangling pointers, or buffer overflows.

10. What is the output of: for(int i=0; i<5; i++) cout << i*i;
A) 01234
B) 1 4 9 16 25
C) 014916
D) 0 1 4 9 16
Answer: C) 014916

Explanation: i runs 0,1,2,3,4. i*i gives 0,1,4,9,16. Since there is no space or separator in the cout statement, the
output is the concatenated string: 014916.
Section 2: HTML, CSS & JavaScript (40 Questions)

1. What does HTML stand for?


A) Hyper Text Markup Language
B) High-Level Text Machine Language
C) Hyper Transfer Markup Language
D) Hyperlink and Text Markup Language
Answer: A) Hyper Text Markup Language

Explanation: HTML stands for Hyper Text Markup Language — the standard language for creating and
structuring web pages.

2. Which tag is used to insert a line break in HTML?


A) <lb>
B) <break>
C) <br>
D) <hr>
Answer: C) <br>

Explanation: <br> is a void/self-closing tag that inserts a single line break. <hr> adds a horizontal rule, not a line
break.

3. What is the correct way to link an external CSS file?


A) <style src='[Link]'>
B) <link rel='stylesheet' href='[Link]'>
C) <css href='[Link]'>
D) <script src='[Link]'>
Answer: B) <link rel='stylesheet' href='[Link]'>

Explanation: The <link> element with rel='stylesheet' placed inside <head> correctly links an external CSS file.

4. What is the difference between id and class in CSS?


A) id can be reused; class cannot
B) class is unique; id can repeat
C) id must be unique per page; class can be shared by multiple elements
D) There is no difference
Answer: C) id must be unique per page; class can be shared by multiple elements

Explanation: An id (#) uniquely identifies a single element. A class (.) can be applied to many elements. id also
has higher specificity than class in CSS.

5. Which CSS property changes text color?


A) font-color
B) text-color
C) color
D) foreground
Answer: C) color

Explanation: The 'color' property sets the foreground (text) color. background-color sets the background.

6. How do you make text bold in HTML?


A) <i>text</i>
B) <bold>text</bold>
C) <strong>text</strong> or <b>text</b>
D) <em>text</em>
Answer: C) <strong>text</strong> or <b>text</b>

Explanation: <b> makes text bold visually. <strong> also makes text bold and carries semantic importance for
screen readers and SEO.

7. What is the default position value in CSS?


A) absolute
B) relative
C) fixed
D) static
Answer: D) static

Explanation: All elements have position: static by default, meaning they flow normally in the document. Offset
properties (top, left, etc.) have no effect on statically positioned elements.

8. What is the purpose of the <meta> tag?


A) Creates a hyperlink
B) Provides metadata about the HTML document (charset, viewport, description, etc.)
C) Embeds a script
D) Defines a section heading
Answer: B) Provides metadata about the HTML document (charset, viewport, description, etc.)

Explanation: <meta> tags go inside <head> and supply metadata consumed by browsers (e.g. charset, viewport
settings) and search engines (e.g. description, keywords).

9. What is the DOM in JavaScript?


A) Document Object Model — a tree representation of the HTML document
B) Data Object Model
C) Document Orientation Model
D) A CSS framework
Answer: A) Document Object Model — a tree representation of the HTML document

Explanation: The DOM is a programming interface that represents the HTML document as a tree of node
objects. JavaScript uses the DOM to dynamically read and manipulate page content and structure.

10. What is the difference between var, let, and const in JavaScript?
A) All are identical
B) var is function-scoped & hoisted; let/const are block-scoped; const cannot be reassigned
C) let is function-scoped; var is block-scoped
D) const can be reassigned once
Answer: B) var is function-scoped & hoisted; let/const are block-scoped; const cannot be
reassigned

Explanation: var declarations are hoisted and function-scoped, causing common bugs. let and const are
block-scoped (ES6+). const prevents reassignment but the object it points to can still be mutated.

11. What does === do in JavaScript?


A) Assigns a value
B) Checks equality with type coercion
C) Checks strict equality (value AND type, no coercion)
D) Checks if a variable exists
Answer: C) Checks strict equality (value AND type, no coercion)
Explanation: === (strict equality) returns true only if both operands have the same type AND value. 5 === '5' is
false because types differ.

12. What is the output of: [Link](typeof null)?


A) 'null'
B) 'undefined'
C) 'object'
D) 'number'
Answer: C) 'object'

Explanation: typeof null returns 'object' — a well-known historical bug in JavaScript that has been kept for
backward compatibility. null is not actually an object.

13. What is an event listener?


A) A CSS rule that watches for hover
B) A function registered to respond to a specific event on an element
C) A server-side handler
D) A JavaScript variable declaration
Answer: B) A function registered to respond to a specific event on an element

Explanation: addEventListener() attaches a callback function to an element that fires whenever the specified
event (click, keyup, etc.) occurs.

14. What is hoisting in JavaScript?


A) Moving CSS to the top of the file
B) JavaScript's behaviour of moving declarations to the top of their scope before execution
C) A way to import modules
D) Compressing JavaScript files
Answer: B) JavaScript's behaviour of moving declarations to the top of their scope before
execution

Explanation: var declarations and function declarations are hoisted. let/const are hoisted but not initialised (they
remain in a Temporal Dead Zone until their line is reached).

15. How do you create an array in JavaScript?


A) var a = {};
B) var a = [];
C) var a = array();
D) var a = new List();
Answer: B) var a = [];

Explanation: Arrays are created with square bracket literal syntax: let arr = [1, 2, 3]; or with new Array(). The
literal [] is the preferred, idiomatic approach.

16. How is a function defined in JavaScript?


A) function myFunc() {}
B) def myFunc():
C) func myFunc() {}
D) void myFunc() {}
Answer: A) function myFunc() {}

Explanation: JavaScript functions can be declared with the function keyword, as arrow functions (() => {}), or as
function expressions (const f = function(){}).
17. What does NaN mean in JavaScript?
A) Not a Number — result of an invalid numeric operation
B) Null and None
C) Negative and Neutral
D) New Array Node
Answer: A) Not a Number — result of an invalid numeric operation

Explanation: NaN (Not a Number) results from operations like parseInt('abc') or 0/0. Notably, typeof NaN is
'number', and NaN !== NaN.

18. What is the result of 2 + '2' in JavaScript?


A) 4
B) '22'
C) NaN
D) 22
Answer: B) '22'

Explanation: The + operator with a string triggers type coercion: 2 is converted to '2', then string concatenation
produces '22'.

19. What is the difference between == and === in JavaScript?


A) No difference
B) == checks strict equality; === uses coercion
C) == allows type coercion; === checks value AND type without coercion
D) === is used only for objects
Answer: C) == allows type coercion; === checks value AND type without coercion

Explanation: 5 == '5' is true (coercion), but 5 === '5' is false. Always prefer === to avoid unexpected coercion
bugs.

20. What is a closure in JavaScript?


A) A way to close the browser window
B) A function that retains access to its outer (enclosing) scope even after the outer function has returned
C) A method to end a loop
D) An error-handling mechanism
Answer: B) A function that retains access to its outer (enclosing) scope even after the outer
function has returned

Explanation: Closures allow inner functions to 'remember' variables from their enclosing scope. They are
fundamental to patterns like data privacy, factory functions, and callbacks.

21. What is the difference between null and undefined in JavaScript?


A) Both are identical
B) undefined means a variable is declared but not assigned; null is an explicit empty/no-value assignment
C) null is a type; undefined is a value
D) undefined only appears in arrays
Answer: B) undefined means a variable is declared but not assigned; null is an explicit
empty/no-value assignment

Explanation: undefined is assigned by JavaScript automatically. null is intentionally assigned by the programmer
to indicate 'no value'. typeof undefined is 'undefined'; typeof null is 'object'.

22. How can you stop form submission in JavaScript?


A) return null;
B) [Link]();
C) [Link]();
D) [Link]();
Answer: C) [Link]();

Explanation: Calling [Link]() inside a submit event handler cancels the default browser behaviour
(sending the form data and refreshing the page).

23. What is a callback function?


A) A function that calls itself
B) A function passed as an argument to another function, to be executed later
C) A function that returns an object
D) A built-in JavaScript method
Answer: B) A function passed as an argument to another function, to be executed later

Explanation: Callbacks are foundational to asynchronous JavaScript. For example: setTimeout(myCallback,


1000) passes myCallback to be called after 1 second.

24. What does setTimeout() do?


A) Repeatedly executes a function at fixed intervals
B) Executes a function once after a specified delay (in ms)
C) Pauses the entire browser
D) Creates a timer element in the DOM
Answer: B) Executes a function once after a specified delay (in ms)

Explanation: setTimeout(fn, delay) schedules fn to run once after delay milliseconds. setInterval() is used for
repeated execution.

25. Which JavaScript function is used to parse JSON?


A) [Link]()
B) [Link]()
C) parseJSON()
D) [Link]()
Answer: B) [Link]()

Explanation: [Link](jsonString) converts a JSON-formatted string into a JavaScript object. [Link]()


does the reverse — converts an object to a JSON string.

26. What is the use of preventDefault()?


A) Stops an event from being created
B) Cancels the default action of a browser event
C) Removes an event listener
D) Prevents a variable from being modified
Answer: B) Cancels the default action of a browser event

Explanation: Common uses: preventing form submission (submit event), preventing page navigation on link click
(click event on <a>), or preventing default key behaviour.

27. How do you select elements by class name in JavaScript?


A) [Link]('.myClass')
B) [Link]('#myClass')
C) [Link]('myClass')
D) [Link]('myClass')
Answer: C) [Link]('myClass')

Explanation: getElementsByClassName() returns an HTMLCollection of all matching elements. Alternatively,


[Link]('.myClass') returns a NodeList.

28. What is event bubbling?


A) An event that fires only on the target element
B) The process by which an event propagates up from the target element through its ancestors in the DOM
C) A CSS animation technique
D) A method to create multiple events simultaneously
Answer: B) The process by which an event propagates up from the target element through its
ancestors in the DOM

Explanation: When an event fires on a child element, it bubbles up through parent elements. This is why a click
on a <button> inside a <div> also triggers any click handler on the <div>.

29. What is the difference between forEach and map in JavaScript?


A) Both return a new array
B) forEach returns a new array; map does not
C) map returns a new transformed array; forEach returns undefined and is used for side effects
D) They are identical
Answer: C) map returns a new transformed array; forEach returns undefined and is used for side
effects

Explanation: Use map() when you need a transformed result array. Use forEach() when you just want to iterate
for side effects (e.g. logging) without needing a return value.

30. What does 'this' refer to in JavaScript?


A) Always refers to the global window object
B) Refers to the object that is the current execution context (depends on how the function is called)
C) Always refers to the DOM element
D) Refers to the previous function
Answer: B) Refers to the object that is the current execution context (depends on how the function
is called)

Explanation: In a method, this is the object. In a regular function (non-strict mode), this is window. In an arrow
function, this is inherited from the surrounding lexical scope.

31. What is the use of z-index in CSS?


A) Sets the zoom level of an element
B) Controls the horizontal positioning
C) Controls the stacking order of positioned elements along the z-axis (depth)
D) Sets element opacity
Answer: C) Controls the stacking order of positioned elements along the z-axis (depth)

Explanation: z-index only works on elements with a position other than static. A higher z-index value places an
element in front of elements with lower values.

32. What does the <script defer> attribute do?


A) Executes the script before HTML is parsed
B) Ignores the script completely
C) Defers script execution until the HTML document has been fully parsed
D) Makes the script load asynchronously without order guarantee
Answer: C) Defers script execution until the HTML document has been fully parsed
Explanation: defer downloads the script in parallel with HTML parsing but executes it only after the DOM is
ready, preserving script order — ideal for scripts that interact with the DOM.

33. How can you centre a div horizontally using CSS?


A) text-align: center on the div
B) margin: 0 auto on the div with a defined width
C) position: center
D) align: center
Answer: B) margin: 0 auto on the div with a defined width

Explanation: margin: 0 auto (with a defined width) is the classic method. Modern alternatives include display: flex
+ justify-content: center on the parent, or display: grid + place-items: center.

34. What is the difference between position: absolute and position: relative?
A) Both are identical
B) relative offsets from its normal position; absolute removes it from normal flow and positions relative to
nearest positioned ancestor
C) absolute is relative to the viewport; relative is relative to the document
D) relative requires a parent with display: flex
Answer: B) relative offsets from its normal position; absolute removes it from normal flow and
positions relative to nearest positioned ancestor

Explanation: position: relative keeps the element in flow but shifts it visually. position: absolute removes it from
the flow entirely and anchors it to the nearest ancestor with a non-static position.

35. How do you make an element responsive in CSS?


A) Use fixed pixel widths
B) Use percentage widths, max-width, media queries, and flexible units like em/rem/vw
C) Use position: absolute on everything
D) Set width: 100px on every screen
Answer: B) Use percentage widths, max-width, media queries, and flexible units like em/rem/vw

Explanation: Responsive design uses fluid grids (%), flexible images (max-width: 100%), media queries
(@media) to adjust layouts, and modern layouts like Flexbox and CSS Grid.

36. What does querySelector() do in JavaScript?


A) Selects all matching elements and returns an array
B) Returns the first element that matches a specified CSS selector
C) Selects elements by tag name only
D) Queries the server for elements
Answer: B) Returns the first element that matches a specified CSS selector

Explanation: [Link]('.myClass') returns only the first match. Use querySelectorAll() to get all
matching elements as a NodeList.

37. How do you apply a transition effect on hover in CSS?


A) Use JavaScript only
B) Add transition property on the element and change the property in :hover selector
C) Use @keyframes only
D) Use transform: hover()
Answer: B) Add transition property on the element and change the property in :hover selector

Explanation: Example: .btn { transition: background 0.3s ease; } .btn:hover { background: blue; } — the transition
property defines what changes animate and how.
38. How do you prevent an event from bubbling in JavaScript?
A) [Link]()
B) [Link]()
C) [Link]()
D) return false only
Answer: B) [Link]()

Explanation: [Link]() stops the event from bubbling up to parent elements.


[Link]() cancels the default browser action but does not stop bubbling.

39. What is the difference between innerText and innerHTML?


A) Both are identical
B) innerText returns plain text (rendered); innerHTML returns the HTML markup including tags
C) innerHTML only works for images
D) innerText parses HTML tags
Answer: B) innerText returns plain text (rendered); innerHTML returns the HTML markup including
tags

Explanation: innerHTML can be used to insert HTML. innerText returns only the visible text. textContent is
similar to innerText but also includes hidden text and doesn't trigger reflow.

40. What does addEventListener('DOMContentLoaded', ...) mean?


A) Fires when all images and resources are fully loaded
B) Fires when the initial HTML document is completely parsed (without waiting for stylesheets/images)
C) Fires every time the DOM changes
D) Fires when a DOM element is clicked
Answer: B) Fires when the initial HTML document is completely parsed (without waiting for
stylesheets/images)

Explanation: DOMContentLoaded fires as soon as the HTML is parsed and the DOM is ready, even if external
resources like images haven't loaded yet. Use [Link] to wait for everything.

End of Answer Sheet — 50 Questions Covered

You might also like