End of Term One Js Exam - Marking Guide
End of Term One Js Exam - Marking Guide
PERIOD: 2HOURS
MARKS: 50
Instructions:
- Do all numbers
- Provide answers from this question paper
A) Compilation Error
B) Runtime error
C) My name is FoeDoe
D) My name is Foe Doe
5) What will be the output for the following code snippet? (Marks 5)
var number1 = 4
var number2 = 6
var number3 =++number1
console.log(number2+number3)
A) 11
B) 10
C) Error
D) None of the above
6) What will be the output of the following code snippet? (Marks 5)
var a = 1;
a = “variable x”;
console.log(a)
A) 1
B) variable x
C) a variable x
D) None of the above
7) Which of the following is the correct output for the following JavaScript code: (Marks 5)
var farmAnimals = [“Cows”,”Sheep”,”Rabbits”,”Goats”]
farmAnimals[0] = “Cammel”;
farmAnimals[1] = “Rabbits”;
console.log(farmAnimals[1])
A) Sheep
B) Rabbit
C) Error
D) None
8) Javascript is a weakly typed language, give the difference between weakly/loosely typed languages
from strongly typed ones (Marks 5)
Weakly Typed (or Loosely Typed) Languages:
1. Implicit Type Conversion:
Weakly typed languages allow for automatic and implicit type conversions. For example, you can perform
operations between different data types without explicitly converting them.
2. Dynamic Typing:
The data type of a variable is not strictly defined. A variable can hold values of different types during its
lifetime.
3. Less Strict Type Checking:
Weakly typed languages are more permissive when it comes to type mismatches. Type errors might not be
caught until runtime.
4. Flexible Variable Usage:
Variables can be used in a more flexible manner, as their types can change dynamically.
Strongly Typed Languages:
1. Explicit Type Conversion (Casting):
In strongly typed languages, explicit type conversion (casting) is required when performing operations
between different data types.
2. Static Typing:
The data type of a variable is strictly defined at compile-time, and it cannot change during runtime.
3. Strict Type Checking:
Strongly typed languages enforce strict type checking, catching type errors at compile-time rather than
runtime. This helps in identifying and preventing type-related bugs early in the development process.
4. Less Flexible Variable Usage:
Variables are more rigid in terms of type, and their types are typically fixed.
9) List with help of an example at least 4 types of data types used in JavaScript (Marks 6)
- Numbers
- var age = 25; // integer
- var height = 5.9; // floating-point number
- String
- var name = "John";
var message = 'Hello, World!';
- Boolean
- var isStudent = true;
var hasJob = false;
- Arrays
- var colors = ['red', 'green', 'blue'];
var numbers = [1, 2, 3, 4, 5];
10) Explain with help of an example at least two non native/complex data types found in JavaScript
(Marks 4)
- Arrays
var numbers = [1, 2, 3, 4, 5];
console.log(numbers[2]);
- Object
- var person = {
- firstName: 'John',
- lastName: 'Doe',
- age: 30,
- address: {
- city: 'Example City',
- zipCode: '12345'
- },
- fullName: function() {
- return this.firstName + ' ' + this.lastName;
- }
};
- Regex
- var pattern = /hello/;
- var text = "Hello, World!";
-
- if (pattern.test(text)) {
- console.log("Match found!");
- } else {
- console.log("Match not found!");
- }
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!Good Luck!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!