Você está na página 1de 7

CONFIDENTIAL 2 CS/CSC305/TEST 2/SEPT ‘17 – JAN ‘18

PART A (20 MARKS)

1. C was originally designed in the early 1970s by ____________.

A. Bill Joy
B. Dennis Ritchie
C. Guido van Rossum
D. Robert Kowalski

2. The format identifier ‘%i” is used for ___________ data type?

A. float
B. double
C. char
D. int

3. What is a pointer in C language?

A. A keyword to declare variables


B. A variable used to store address of an instruction
C. A variable used to store address of other variable
D. A variable used to store address of a structure

4. What is the output of this C code?

#include <stdio.h>
Void main()
{
double i = 0;
for ( i = 0.0; i < 5.0; i++)
Printf(“Hello World”);
}

A. Hello World is printed 4 times


B. Hello World is printed 5 times
C. Hello World is printed infinitely
D. Run time error

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 3 CS/CSC305/TEST 2/SEPT ‘17 – JAN ‘18

5. Given below are the characteristics of ____________.

i. Very high-level string processing


ii. Very high-level graphical user interface support
iii. Dynamic typing

A. Concurrent programming
B. Imperative programming
C. Scripting languages
D. Declarative programming

6. Choose One(1) statement that correctly initialized a tuple in Python.

A. Nombor_series = “10, 20, 30, 40, 40”


B. Nombor_series = [10, 20, 30, 40, 40]
C. Nombor_series = {10, 20, 30, 40, 40}
D. Nombor_series = (10, 20, 30, 40, 40)

7. Keyword ____________ is used to construct objects in Python.

A. self.
B. __init__
C. def
D. this.

8. Global variables can known as ___________________ in Java programming.


A. class variables
B. heap variables
C. static variables
D. dynamic variables

9. ______________is a key concept, enabling an object of a subclass to be treated like an


object of its superclass.
A. Classes and subclasses
B. Data abstraction
C. Inclusion polymorphism
D. Parametric polymorphism

10. Composite values in JAVA are____________________.


A. dictionaries
B. objects
C. arrays
D. lists

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 4 CS/CSC305/TEST 2/SEPT ‘17 – JAN ‘18

ANSWER FOR PART A (20 MARKS)

1. =A= =B= =C= =D= 6. =A= =B= =C= =D=

2. =A= =B= =C= =D= 7. =A= =B= =C= =D=

3. =A= =B= =C= =D= 8. =A= =B= =C= =D=

4. =A= =B= =C= =D= 9. =A= =B= =C= =D=

5. =A= =B= =C= =D= 10. =A= =B= =C= =D=

PART B (30 MARKS)

QUESTION 1

a) Identifier is a user-define word used to represent the programming elements such as


variables, functions, arrays and structure. Give TWO(2) rules for constructing identifiers in
C language.
(2 marks)
1) The first character in an identifier must be an alphabet or an underscore & can be followed
only by any number alphabets or digit or underscore.
2) They must not begin with a digit.
3) Uppercase and lowercase letter are distinct. That is identifier are case sensitive.
4) Commas or blank spaces are not allowed within an identifier.

b) Berjaya Sdn. Bhd. wants to keep the record of their employees' bonus salary. The bonus
will be calculated based on the level of the employee in table below.

Level Bonus Percentage


1 – Manager 25%
2 – Executive 15%

Given is the record information for an employee. Write the C program based on the
questions to find the bonus for 50 employees.

struct Employee
{
int id;
int position;
float salary;
};
struct Employee emp[50];

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 3 CS/CSC305/TEST 2/SEPT ‘17 – JAN ‘18

i) Write a procedure named read_data() to read information of the employees.

void read_data(int i)
{
scanf("%d" , &emp[i].id);

scanf("%d" , &emp[i].position);

scanf("%f" , &emp[i].salary);
}
(2 marks)

ii) Write a function named calc_bonus() to calculate bonus of the employee.

float calc_bonus(int j)
{
float bonus = 0.0;

if ( emp[j].position == 1) }
bonus = 0.25 * emp[j].salary; }
else
if ( emp[j].position == 2) }
bonus = 0.15 * emp[j].salary; }
return bonus;
}
(3 marks)

iii) Write a main program to do:


 Input the appropriate data for each employee in the company.
 Display the employee ID with the bonus amount for each employee.

for (int i=0; i<50; i++)


read_data(i);

for (int j=0; j<50; j++)


{
printf( "\nEmployee: %d" , emp[j].id );
printf( " Bonus: RM %f" , calc_bonus(j));
}

(3 marks)

QUESTION 2
© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL
CONFIDENTIAL 4 CS/CSC305/TEST 2/SEPT ‘17 – JAN ‘18

a) Write the output from the following program written in Java.

class KAG{
public void kjm()
{
System.out.println ("kjm() method of parent class"); }

public void kbm()


{
System.out.println ("kbm() method of parent class"); }
}

class FSKM extend KAG{

public void kjm()


{
System.out.println ("kjm() method of child class"); }

public void ktm()


{
System.out.println ("ktm() method of child class"); }

public static void main (String[]args){


KAG obj = new FSKM();
obj.kjm();
obj.kbm();
obj.ktm();
}}

(6 marks)

kjm() method of child class


kbm() method of parent class
ktm() method of child class

b) Differentiate between method overloading and method overriding


(4 marks)
Method overloading Method overriding
When a method in a class having the same When a method in a class having the same
method name but with different arguments method name with same arguments

QUESTION 3

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 5 CS/CSC305/TEST 2/SEPT ‘17 – JAN ‘18

a) State whether the following statements regarding scripting languages are TRUE or
FALSE.
(2 marks)
1. Scripting languages provide support for variables, procedures and commands.

2. Almost all scripting languages employ regular expressions in string matching operations.

(ANSWER TRUE OR FALSE)

1. =TRUE= =FALSE= 2. =TRUE= =FALSE=

b) Given the following C function to find Fibonacci number. Rewrite the function in Python
using recursive.

int recFibN(int n)
{
if (n < 2)
return n;
else
return recFibN(n-1) + recFibN(n-2);
}

def recFibN(n):
if n < 2:
return n
else:
return recFibN(n-1) + recFibN(n-2)

(4 marks)

c) Given is class Cake written in Java. Rewrite the class in Python.

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 6 CS/CSC305/TEST 2/SEPT ‘17 – JAN ‘18
public class Cake
{
private String type;
private int weight;

public Cake(String type, int weight)


{
this.type = type;
this.weight = weight;
}

public void displayCake()


{
System.out.println( "Name : " + this.type +
" Weight : " + this.weight);
}
}

class Cake:

def __init__(self, type, weight):


self._type = type
self._weight = weight

def displayCake(self):
print "Name : ", self._type, ", Weight: ", self._weight

(4 marks)

END OF ANSWER PAPER

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL

Você também pode gostar