Professional Documents
Culture Documents
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
1 / 7
2. You are required to model a new user-defined object type, Circle. A Circle object has a
radius property and two methods, area and circumference.
b. Implement a method area(), which gets the value of the radius and returns the area of the
circle, that is (π * radius 2 ) and π = 3.14. Note that you could use Math.PI instead of 3.14.
c. Implement a method circumference(), which gets the value of the radius and returns the
circumference of the circle, that is (2π * radius).
d. Implement a method display(), which displays in an alert box the information about the
object as shown below.
e. Test the constructor and the methods implemented in b, c and d on Circle objects of radius
values 2, 50 and 100.
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
2 / 7
3. A new Institute object type is to created. It has the following properties:
name: a string variable that will hold the name of the Institute.
location: a string variable that will hold the location (country name) of the Institute.
listOfDept: an Array of strings, which holds the names of the departments inside the Institute.
1. Define the constructor function Institute (aName, aLocation, aNoOfDept) which will
set the properties and methods of the newly created Institute objects according to the above
description. Note that aNoOfDept defines the number of elements to be stored in the array
listOfDept.
2. Ask the user about the number of departments inside an Institute.
3. Create an Institute instance named myInstitute, and initialize its properties as follows:
name = 'MIT', location= 'USA', noOfDept = number of departments entered in step 1.
4. Fill in the departments’ names of myInstitute using the method addDept().
5. Display the list of departments of myInstitute using the method showDept().
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
3 / 7
4. You are required to model a mountain with a new user-defined object type, Mountain. A
Mountain object has two properties, name and height (in meters).
a. Write a constructor function which takes two arguments and define two instance variables,
name and height.
b. Now implement a method toString(), which gets the values of the name and height
properties of a Mountain object and outputs them in an alert box.
c. Create a Mountain object which represents Mount Everest (name-->'Everest' and
height-->8850) and test the method you've created in (b).
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
4 / 7
5. Given the following JavaScript codes:
Case (a)
function f()
{
var b, c;
a = a * 2;
document.write ('Value2 of a is’ + a + ’<BR>’);
}
var a = 100;
document.write(' Value1 of a is ' + a + ’<BR>’);
f();
document.write ('Value3 of a is’ + a + ’<BR>’);
Case (b)
function f()
{
var a, b;
a = a * 2;
document.write ('Value2 of a is’ + a + ’<BR>’);
}
var a = 100;
document.write(' Value1 of a is ' + a + ’<BR>’);
f();
document.write ('Value3 of a is’ + a + ’<BR>’);
5 / 7
6. What is the output of the following JavaScript code?
function function1() {
var a,b;
a =10;
b = 20;
document.write(a + '<BR>');
document.write(b + '<BR>') };
var b,c;
b = 30;
c = 40;
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
7. What are the values of x, y, a and b after executing the following code?
var x = 22;
var y = x;
y = y + 1;
var a = new Array (10,20,30);
var b = new Array ('A','B','C');
a = b;
b[2] = 100;
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
6 / 7
8. What is the output of the following Java Script code?
var x = 50;
var y = x;
var date1 = new Date(2010,9,21);
var date2 = date1;
x = x + y;
date1.setMonth(11);
document.write(x +'<BR>');
document.write(y + '<BR>');
document.write(date1.getMonth()+'<BR>');
document.write(date2.getMonth()+'<BR>');
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
function AddTwoNumbers()
{
var a = 7;
b = 4;
c = a + b;
document.write('For this second display the value of c
is ' + c + '<BR>');
}
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
..........................................................................................................................................
(This question is taken from M150B final exam, Spring 2009)
7 / 7