MCQs Of Class 19
Introduction to HTML5 APIs
Geolocation API MCQs
1. Which of the
following methods is used to obtain the user's geographical location in the
Geolocation API?
A) getPosition()
B) getCurrentLocation()
C) getCoordinates()
D) getCurrentPosition()
Answer: D) getCurrentPosition()
Explanation: getCurrentPosition() is the correct
method to retrieve the current geographical location of the user.
2. What does the
Geolocation API return when it successfully retrieves a user's location?
A) Latitude only
B) Longitude only
C) Latitude and
Longitude
D) Altitude only
Answer: C) Latitude and Longitude
Explanation: The Geolocation API returns both latitude and
longitude when the location is successfully retrieved.
3. Which of the
following is a required permission to access the user's location using the
Geolocation API?
A) User consent
B) HTTPS connection
C) GPS hardware access
D) All of the above
Answer: D) All of the above
Explanation: The Geolocation API requires user consent, an
HTTPS connection, and access to the GPS hardware.
4. What is the default
behavior of the Geolocation API if the user's location cannot be retrieved?
A) It provides a
fallback location
B) It retries the request
indefinitely
C) It invokes the error
callback function
D) It displays a
generic message
Answer: C) It invokes the error callback function
Explanation: If the location cannot be retrieved, the error
callback function is invoked.
5. In the Geolocation
API, which method allows you to track changes in the user's position?
A) watchPosition()
B) trackPosition()
C) monitorPosition()
D) observePosition()
Answer: A) watchPosition()
Explanation: watchPosition() is used to continuously
monitor and update the user's position as it changes.
6. What is the range of
valid values for latitude in the Geolocation API?
A) 0 to 180
B) -90 to 90
C) -180 to 180
D) -90 to 0
Answer: B) -90 to 90
Explanation: Latitude values range from -90 to 90 degrees.
7. Which property of
the position object in the
Geolocation API provides the accuracy of the location?
A) position.coords.accuracy
B) position.coords.latitude
C) position.coords.altitude
D) position.coords.longitude
Answer: A) position.coords.accuracy
Explanation: accuracy indicates the
accuracy level of the latitude and longitude provided.
8. What is the correct
way to handle errors when using the Geolocation API?
A) Use the onError event handler
B) Use the catch method with promises
C) Provide an error
callback in getCurrentPosition or watchPosition
D) Both B and C
Answer: D) Both B and C
Explanation: Errors can be handled using an error callback
or promise catch method.
9. In the Geolocation
API, what is the typical unit for accuracy?
A) Meters
B) Kilometers
C) Degrees
D) Feet
Answer: A) Meters
Explanation: The accuracy of the location is typically
measured in meters.
10. What
will happen if geolocation is disabled in the browser?
A) The browser will
automatically enable geolocation.
B) The geolocation
request will fail, and an error will be returned.
C) A default location
will be used.
D) Nothing will happen.
Answer: B) The geolocation request will fail, and an
error will be returned.
Explanation: If geolocation is disabled, the request will
fail, and an error will be returned.
Canvas API MCQs
11. Which
of the following is used to obtain the drawing context of a <canvas> element?
A) getContext('2d')
B) getCanvasContext()
C) getDrawingContext()
D) createContext()
Answer: A) getContext('2d')
Explanation: getContext('2d') is used to obtain
the 2D drawing context for drawing on the canvas.
12. What
does the beginPath() method do in the
Canvas API?
A) Starts a new line
B) Starts a new drawing
shape
C) Clears the canvas
D) Ends the drawing
session
Answer: B) Starts a new drawing shape
Explanation: beginPath() is used to start a
new path, so subsequent drawing commands do not affect the previous one.
13. Which
method in the Canvas API is used to fill a shape with color?
A) fill()
B) stroke()
C) draw()
D) paint()
Answer: A) fill()
Explanation: The fill() method is used to
fill the current path with the current fill style.
14. How
can you draw a circle in the Canvas API?
A) ctx.arc(x, y, radius, startAngle, endAngle)
B) ctx.lineTo(x, y)
C) ctx.drawCircle(x, y, radius)
D) ctx.circle(x, y, radius)
Answer: A) ctx.arc(x,
y, radius, startAngle, endAngle)
Explanation: The arc() method is used to
draw circles and arcs.
15. Which
property is used to set the line width in the Canvas API?
A) lineWidth
B) strokeWidth
C) width
D) lineSize
Answer: A) lineWidth
Explanation: The lineWidth property sets the
width of the lines drawn on the canvas.
16. Which
method is used to draw a rectangle on the canvas?
A) drawRect(x, y, width, height)
B) ctx.rect(x, y, width, height)
C) ctx.drawRectangle(x, y, width, height)
D) ctx.fillRect(x, y, width, height)
Answer: B) ctx.rect(x,
y, width, height)
Explanation: rect() method is used to
define a rectangle path, but fillRect() is used to fill it.
17. How
do you set the canvas background color in the Canvas API?
A) ctx.fillStyle = 'color';
B) ctx.backgroundColor = 'color';
C) ctx.background = 'color';
D) ctx.color = 'color';
Answer: A) ctx.fillStyle
= 'color';
Explanation: fillStyle sets the color or
pattern used for filling shapes.
18. What
is the default color of the stroke in the Canvas API?
A) Red
B) Black
C) Blue
D) White
Answer: B) Black
Explanation: The default stroke color is black.
19. Which
method is used to close a path in the Canvas API?
A) close()
B) closePath()
C) endPath()
D) finishPath()
Answer: B) closePath()
Explanation: closePath() closes the current
path by drawing a straight line back to the starting point.
20. Which
of the following is used to clear a specified area of the canvas?
A) clearRect(x, y, width, height)
B) clearCanvas()
C) erase(x, y, width, height)
D) resetCanvas()
Answer: A) clearRect(x,
y, width, height)
Explanation: clearRect() method is used to
clear a rectangular area on the canvas.
Combination of Geolocation and Canvas MCQs
21. How
do you use the Geolocation API to get the user's current position?
A) navigator.geolocation.watchPosition()
B) navigator.geolocation.getPosition()
C) navigator.geolocation.getCurrentPosition()
D) navigator.geolocation.retrievePosition()
Answer: C) navigator.geolocation.getCurrentPosition()
Explanation: getCurrentPosition() is used to fetch
the user's current position.
22. In
the Canvas API, how can you change the stroke color to red?
A) ctx.strokeStyle = 'red';
B) ctx.setStrokeColor('red');
C) ctx.setColor('red');
D) ctx.strokeColor = 'red';
Answer: A) ctx.strokeStyle
= 'red';
Explanation: strokeStyle is used to set the
color of the strokes in the Canvas API.
23. What
is the method used to retrieve the current coordinates of the user in the
Geolocation API?
A) position.getCoordinates()
B) navigator.getCoordinates()
C) position.coords
D) navigator.coords
Answer: C) position.coords
Explanation: position.coords contains the
latitude, longitude, and other information of the user's location.
24. In
the Canvas API, how would you begin drawing a line from the current position to
a new position?
A) ctx.moveTo(x, y)
B) ctx.lineTo(x, y)
C) ctx.drawLine(x, y)
D) ctx.line(x, y)
Answer: A) ctx.moveTo(x,
y)
Explanation: moveTo() moves the
"pen" to a new position without drawing, starting a new line from
there.
25. Which
method would you use to continuously track the user's location and draw it on a
canvas in real-time?
A) navigator.geolocation.watchPosition() and ctx.clearRect()
B) navigator.geolocation.getCurrentPosition() and ctx.lineTo()
C) navigator.geolocation.getPosition() and ctx.moveTo()
D) navigator.geolocation.retrieve() and ctx.drawPosition()
Answer: A) navigator.geolocation.watchPosition() and ctx.clearRect()
Explanation: watchPosition() is used to
continuously track the user's location, and clearRect() is used to clear the previous position.
26. Which
of the following is the correct way to handle errors when using the Geolocation
API?
A) navigator.geolocation.onError()
B) navigator.geolocation.error()
C) navigator.geolocation.getCurrentPosition(success, error)
D) navigator.geolocation.errorCallback()
Answer: C) navigator.geolocation.getCurrentPosition(success,
error)
Explanation: The correct way to handle errors is to provide
a second callback function that handles the error in getCurrentPosition().
27. Which
of the following is a reason the Geolocation API might fail to retrieve the
user's position?
A) The user has denied
location access
B) The browser is not
running in secure mode (HTTPS)
C) The device does not
have a GPS chip
D) All of the above
Answer: D) All of the above
Explanation: Location access might fail due to the user
denying permission, the browser not using HTTPS, or the lack of GPS hardware on
the device.
28. What
is the return type of getCurrentPosition() in the Geolocation
API?
A) A promise
B) A location object
C) An error object
D) A position object
Answer: D) A position object
Explanation: getCurrentPosition() returns a position
object containing coordinates and other information.
29. Which
method is used to stop tracking the user's position once it has started
with watchPosition()?
A) navigator.geolocation.stopTracking()
B) navigator.geolocation.clearWatch()
C) navigator.geolocation.endWatch()
D) navigator.geolocation.abortWatch()
Answer: B) navigator.geolocation.clearWatch()
Explanation: clearWatch() is used to stop
watching for location changes when using watchPosition().
30. Which
of the following is a valid way to retrieve the altitude in the Geolocation
API?
A) position.coords.altitude
B) position.altitude
C) position.coords.location
D) navigator.geolocation.altitude
Answer: A) position.coords.altitude
Explanation: The altitude is part of the coords object in the position object returned by
the Geolocation API.
31. Which
of the following browsers supports the Geolocation API?
A) Internet Explorer
10+
B) Firefox 3.5+
C) Chrome 5+
D) All of the above
Answer: D) All of the above
Explanation: Most modern browsers, including Internet
Explorer 10+, Firefox 3.5+, and Chrome 5+, support the Geolocation API.
32. What
kind of value does the accuracy property in the
Geolocation API represent?
A) The time it took to
retrieve the location
B) The horizontal
distance from the actual location
C) The vertical
distance from the actual location
D) The user's speed
Answer: B) The horizontal distance from the actual
location
Explanation: The accuracy property indicates
the radius in meters within which the actual position is likely to be.
33. Which
of the following is the correct callback function format for handling
successful location retrieval in the Geolocation API?
A) function(position) { ... }
B) function(error) { ... }
C) function(success, position) { ... }
D) function(position, success) { ... }
Answer: A) function(position)
{ ... }
Explanation: The success callback for getCurrentPosition() takes the position object as an argument.
34. What
does the timeout option in the
Geolocation API specify?
A) The time to wait for
the user's GPS to be found
B) The maximum time to
wait for a successful response before triggering the error callback
C) The maximum number
of position retries
D) The time to display
the geolocation request to the user
Answer: B) The maximum time to wait for a successful
response before triggering the error callback
Explanation: The timeout option defines how
long to wait for the position before the error callback is triggered.
35. What
does the enableHighAccuracy option in the
Geolocation API do?
A) Provides the fastest
location results
B) Provides the most
accurate location results, even at the cost of battery and time
C) Prevents location
tracking if the GPS is not enabled
D) It has no effect on
the location data
Answer: B) Provides the most accurate location results,
even at the cost of battery and time
Explanation: Setting enableHighAccuracy to true requests the most accurate location, which can take longer
to retrieve.
Canvas API MCQs
36. Which
method is used to draw text on the canvas in the Canvas API?
A) ctx.writeText()
B) ctx.text()
C) ctx.fillText()
D) ctx.drawText()
Answer: C) ctx.fillText()
Explanation: fillText() is used to draw
filled text on the canvas at a specific position.
37. Which
Canvas API method is used to create a path that can be drawn?
A) ctx.beginPath()
B) ctx.drawPath()
C) ctx.path()
D) ctx.createPath()
Answer: A) ctx.beginPath()
Explanation: beginPath() starts a new path,
allowing you to draw shapes without affecting previous ones.
38. Which
of the following Canvas API methods will create a filled circle?
A) ctx.fillCircle(x, y, radius)
B) ctx.arc(x, y, radius, 0, Math.PI * 2, false)
C) ctx.drawCircle(x, y, radius)
D) ctx.circle(x, y, radius)
Answer: B) ctx.arc(x,
y, radius, 0, Math.PI * 2, false)
Explanation: arc() is used to define
the geometry of a circle, and fill() can be used to fill
it.
39. What
is the default value for the lineWidth property in the Canvas API?
A) 0.5
B) 1
C) 5
D) 10
Answer: B) 1
Explanation: The default value for lineWidth is 1, meaning the line thickness is 1 pixel.
40. Which
Canvas API method is used to set the font of the text?
A) ctx.setFont()
B) ctx.font()
C) ctx.setTextStyle()
D) ctx.fontStyle()
Answer: B) ctx.font()
Explanation: font() is used to specify
the font size, family, and style for text in the Canvas API.
41. Which
of the following will draw a line from the current position to the specified
position (50, 50)?
A) ctx.moveTo(50, 50)
B) ctx.drawLine(50, 50)
C) ctx.lineTo(50, 50)
D) ctx.path(50, 50)
Answer: C) ctx.lineTo(50,
50)
Explanation: lineTo() draws a line from
the current position to the specified position.
42. Which
method in the Canvas API is used to create a rectangular shape?
A) ctx.createRect()
B) ctx.fillRect()
C) ctx.rect()
D) ctx.drawRect()
Answer: C) ctx.rect()
Explanation: rect() is used to define a
rectangular path, but it doesn't fill it. fillRect() is used to fill the rectangle.
43. How
do you apply a gradient to a shape in the Canvas API?
A) ctx.applyGradient()
B) ctx.createGradient()
C) ctx.setGradient()
D) ctx.fillGradient()
Answer: B) ctx.createGradient()
Explanation: To create a gradient, you use createLinearGradient() or createRadialGradient() depending on the type of gradient you want.
44. What
does the save() method do in the
Canvas API?
A) Saves the current
state of the canvas, allowing for restoring later
B) Saves the drawing to
an external file
C) Saves the canvas as
an image
D) Saves the canvas
settings permanently
Answer: A) Saves the current state of the canvas,
allowing for restoring later
Explanation: save() saves the current
canvas state (including transformations, styles, etc.), and restore() can be used to revert to that saved state.
45. What
does ctx.clearRect() do in the Canvas
API?
A) Clears the entire canvas
B) Clears a rectangular
area of the canvas
C) Clears all paths on
the canvas
D) Clears the current
drawing color
Answer: B) Clears a rectangular area of the canvas
Explanation: clearRect() removes a specified
rectangle area from the canvas, making it transparent.
46. Which
method is used to rotate the canvas in the Canvas API?
A) ctx.rotate()
B) ctx.rotateCanvas()
C) ctx.canvasRotate()
D) ctx.transformRotate()
Answer: A) ctx.rotate()
Explanation: rotate() applies a rotation
transformation to the canvas context.
47. What
is the purpose of ctx.globalAlpha in the Canvas API?
A) Controls the
transparency of the canvas
B) Sets the background
opacity
C) Controls the alpha
transparency of text only
D) Adjusts the global
blending mode
Answer: A) Controls the transparency of the canvas
Explanation: globalAlpha sets the
transparency level of all drawing operations on the canvas.
48. How
do you make the background of the canvas transparent?
A) ctx.transparent()
B) ctx.clear()
C) Set the background
color to 'transparent'
D) It is transparent by
default
Answer: D) It is transparent by default
Explanation: The canvas background is transparent by
default. If you want to set a specific color, you need to fill the entire
canvas with that color.
49. Which
of the following is true about the beginPath() method in the Canvas API?
A) It begins a new
shape by creating a new path
B) It draws the current
shape
C) It ends the current
drawing path
D) It fills the current
path
Answer: A) It begins a new shape by creating a new path
Explanation: beginPath() starts a new
drawing path, meaning previous paths do not affect the new drawing.
50. What
is the default canvas width and height?
A) 300px x 150px
B) 500px x 500px
C) 600px x 400px
D) 800px x 600px
Answer: A) 300px x 150px
Explanation: The default size for a canvas element is 300px
by 150px unless specified otherwise.
