Sunteți pe pagina 1din 42

EXAMPLE PAPER

This example examination paper consists of 40 pages.

GENERAL INSTRUCTIONS

1. In the examination hall: answer all the questions on the multiple


choice answer sheet provided. (Remember to take a pencil with!).
There will be 2 empty pages at the back of the paper that you may
use for rough work.

[Please turn the page]

1
ICT2612
Java 2 Example paper 2014

Section A [50 marks]


Java: Solutions

1. Study the code below and indicate what the value of mark will be:

int test1 = 50, test2 = 60;


float mark = test1/test2;

(1) 0.0
Reason: division of two integer values will always display 0.0
(2) 2
Reason: division of two integer values will display only the trun-
cated integer value without the decimal.
(3) 2.0
Reason: division of two integer values will display only the trun-
cated value.
(4) 2.5
Reason: division of two integer values, converted to a float value,
will convert the integer values to float and display the answer
more accurately.

1. C

2. Indicate which of the following line of code has a syntax error?

1 boolean outcome = TRUE;


2 int test1 = 1;
3 double solution = 1.234e2;
4 float budget = 1.23f;
5 long result = 123_456_789;

(1) Line 1
(2) Line 2
(3) Line 3
(4) Line 4
(5) Line 5

2. A

[Please turn the page]


ICT2612
Java 3 Example paper 2014

3. Study the code below and indicate what will the result of d answer
will be:

float f_answer = (float) 123.4;


double d_answer = (int)f_answer;

(1) 123
(2) 124
(3) 123.0
(4) 124.0

3. C

4. Study the code below and indicate what the values will be for val1
and val2 after the code is executed.

double val1 = 4.6;


val1++;
double val2 = val1++ - val1++;

(1) val1:7 val2:-1

(2) val1:8 val2:-1

(3) val1:5.6 val2:-1.0

(4) val1:7.6 val:-1.0

4. D

5. Study the code below and indicate what the value of answer will be
after the code has executed:

float answer;
int x = 5;
int y = 6;
int z = 4;

[Please turn the page]


ICT2612
Java 4 Example paper 2014

int k = 2;
answer = (x + y) * z / k;

(1) 14.5
(2) 22
(3) 17
(4) 17.0

5. D

6. The formula to calculate the are of a circle is:


A = r2

Or translated as PI times the square of the radius.

Indicate which of the code below is the correct code for the above
formula:

(1) double PI, A,r;


r = 5;
A = PI * r * r;
(2) double PI, A,r;
r = 5;
A = PI * r^2;
(3) double A,r;
r = 5;
A = Math.PI * r * r;
(4) double A,r;
r = 5;
A = Math.PI * sqr(r);

6. C

Study the two sections of code below, the one section is for the code
for the class Student and the second section of code is taken from
the main program ActivityMain.
Answer questions 7 to 9.

[Please turn the page]


ICT2612
Java 5 Example paper 2014

public class Student {


String name, surname, course;
static int numStudents;

public Student(String n, String s, String c) {


this.name = n;
this.surname = s;
this.course = c;
numStudents = numStudents + 1;
}

public String getStudentName(){


return this.surname + ", " + this.name.substring(0);
}

public String getQualification(){


return this.course;
}

----------------------------------------------

Student student[] = new Student[100];

student[0] = new Student("John","Flanagan","98806");


student[1]= new Student("Peter","Mkize","98806");
student[2]= new Student("Ben","Naidoo","NDINL");

int total = Student.numStudents;

String message="";
for (int i=0; i < Student.numStudents; i++)
message = message + student[i].getStudentName()+"\n";

7. What will the value of total be after the code is executed?


(1) 2
(2) 3
(3) 100

[Please turn the page]


ICT2612
Java 6 Example paper 2014

(4) 103

7. B

8. What will the value of message be after the code is executed?

(1) Flanagan, John\nMkize, Peter\nNaidoo, Ben\n


(2) Flanagan, John
Mkize, Peter
Naidoo, Ben
(3) Flanagan, JohnMkize, PeterNaidoo, Ben
(4) Error message. NullPointerException. The array is declared for a 100 e

8. B

9. The user adds the following code to the main program:

int total98806 = countQ("98806",student);

This code calls a method countQ. Indicate which of the code below
will NOT correctly count the number of students registered for the
qualification 98806.

(1) private static int countQ(String Q, Student[] student) {


int total = 0;
for (int i=0; i < Student.numStudents; i++){
if (student[i].getQualification().equals(Q))
total++;
}

return total;
}
(2) private static int countQ(String Q, Student[] student) {
int total = 0;
for (int i=0; i < student.length; i++){
if (student[i].getQualification().equals(Q))
total++;
}

[Please turn the page]


ICT2612
Java 7 Example paper 2014

return total;
}
(3) private static int countQ(String Q, Student[] student) {
int total = 0;
for (int i=0; i < 3; i++){
if (student[i].getQualification()==Q)
total++;
}

return total;
}
(4) private static int countQ(String Q, Student[] student) {
int total = 0;
for (int i=0; i < 3; i++){
if (student[i].getQualification().equals(Q))
total++;
}

return total;
}

9. B

10. Indicate in which of the code below will the value of pass be "Yes".

(1) String pass = "No";


String studNum = "123-456-789";
String level = "1";
boolean isStudent = level == "1";

if (studNum.substring(0,1)="1" and isStudent)


pass = "Yes";
(2) String pass = "No";
String studNum = "123-456-789";
String level = "1";
boolean isStudent = level == "1";

if (studNum.substring(0,1)=="1" && isStudent)


pass = "Yes";

[Please turn the page]


ICT2612
Java 8 Example paper 2014

Reason: The coding is correct and there are no logical errors.


(3) String pass = "No";
String studNum = "123-456-789";
String level = "1";
boolean isStudent = level == "1";

if (studNum.substring(0,1).equals("1") && isStudent)


pass = "Yes";
(4) String pass = "No";
String studNum = "123-456-789";
String level = "1";
boolean isStudent = level && level == "1";

if (studNum.substring(0,1)=="1" && isStudent)


pass = "Yes";

10. C

11. Study the code below and indicate what the values will be for hasMore,
total and place after the program has executed.

boolean hasMore = true;


int[] numbers = {1,4,5,4,6,7};
int place = numbers.length-1, total = 0;

while (hasMore)
{
total = total + numbers[place];
place--;
if (place==0) hasMore = false;
}

(1) total: 27
place: 0
hasMore: false

(2) total: 26
place: 0
hasMore: false

[Please turn the page]


ICT2612
Java 9 Example paper 2014

(3) total: 27
place: -1
hasMore: false

(4) total: 27
place: -1
hasMore: true

11. B

Study the incomplete code below that is used to create the class Car.
Answer questions 12 to 15 that follow.

public class Car {

//instance variables
String make, model, year;
double price;

//first constructor
public Car(String ma, String mo, String y, float p)
{
//(...i...)
}

//second constructor
public Car(String make, String model, float price)
{
this.make = make;
this.model = model;
this.price = sell_price_special(price);

private double sell_price_special(float p) {

//(...ii...)

}//sell_price

[Please turn the page]


ICT2612
Java 10 Example paper 2014

public String display(){


String show = this.make + " R" + this.price;
return show;
}//display

}//Car

[Please turn the page]


ICT2612
Java 11 Example paper 2014

12. (i): Indicate which of the following will correctly link the variables for
the first constructor to the instance variables.

(1) this.make = make;


this.model= model;
this.year = year;
this.price = sell_price_special(price);

(2) this.ma = ma;


this.mo= mo;
this.y = y;
this.p = sell_price_special(p);
(3) this.make = ma;
this.model= mo;
this.year = y;
this.price = sell_price_special(p);
(4) this.make = make;
this.model= model;
this.year = year;
this.p = sell_price_special(price);

12. C

13. (ii): Indicate which of the following code will correctly calculate the
discount of 10% on sale items in sell price special() method.

(1) return p * .1;

(2) return (double)(p / 90);

(3) return p * .1;

(4) return (float)(p / 90);

13. C

[Please turn the page]


ICT2612
Java 12 Example paper 2014

14. Indicate which of the following code will correctly create two new in-
stances (car1 and car2) of the class Car.

(1)
Car car1 = new Car("Toyota", "2.0 Standard 6MT", "2014",324800);
car2 = new Car("Toyota","2.0 High 6MT",365200);

(2)
Car car1 = new Car("Toyota", "2.0 Standard 6MT", "2014",324800);
Car car2 = new Car("Toyota","2.0 High 6MT",365200);

(3)
Car = new Car();
car1 = new Car("Toyota", "2.0 Standard 6MT", "2014",324800);
car2 = new Car("Toyota","2.0 High 6MT",365200);

(4)
Car car1 = new Car(ma="Toyota", mo="2.0 Standard 6MT",
y="2014",p=324800);
Car car2 = new Car(make="Toyota",model="2.0 High 6MT",
price=365200);

14. B

15. Indicate what the value will be of display after the following code is
executed:

String display = car2.display();

(1) Toyota R36520.0

(2) Toyota 2.0 High 6MT

(3) Toyota 2.0 High 6MT R36520.0

(4) Toyota R36520.00

[Please turn the page]


ICT2612
Java 13 Example paper 2014

(5) Error message: The actual and formal argument list differ
in length.

15. A

[Please turn the page]


ICT2612
Java 14 Example paper 2014

Study the incomplete code below and answer questions 16 to 19 that


follow.

//declare arrays and allocate values to the arrays.


String[] modules = new String[3];
String[] levels = new String[3];

modules[0] = "ICT1511";
modules[1] = "ICT2611";
modules[2] = "ICT3611";

levels[0] = "1";
levels[1] = "2";
levels[2] = "3";

16. Indicate which of the following statements can replace the array dec-
larations and the 8 statements that are used to allocate values to
arrays names and surnames.

(1) String[] modules = new String {"ICT1511","ICT2611","ICT3611"};


String[] levels = new String {"1","2","3"};

(2) String[] modules = {"ICT1511","ICT2611","ICT3611"};


String[] levels = {"1","2","3"};

(3) String[3] modules = {"ICT1511","ICT2611","ICT3611"};


String[3] levels = {"1","2","3"};

(4) String[3] modules = new String {"ICT1511","ICT2611","ICT3611"};


String[3] levels = new String{"1","2","3"};

16. B

[Please turn the page]


ICT2612
Java 15 Example paper 2014

17. Indicate which of the following code will correctly combine the two
arrays modules and levels and initialise the array progMods.

(1) for (int i=0; i<2; i++){


progMods[i] = modules[i] + " " + levels[i];
}

(2) for (int i=0; i=3; i++){


progMods[i] = modules[i] + " " + levels[i];
}

(3) for (int i=4; i<0; i--){


students[i] = names[i] + " " + surnames[i];
}

for (int i=0; i=3; i--){


progMods[i] = modules[i] + " " + levels[i];
}

(4)
for (int i=0; i<4; i++){
progMods[i] = modules[i] + " " + levels[i];
}

(5) for (int i=0; i<3; i++){


progMods[i] = modules[i] + " " + levels[i];
}

17. E

18. Indicate which of the following code will correctly calculate the length
of the array progMods.

(1) int len = progMods.length();

[Please turn the page]


ICT2612
Java 16 Example paper 2014

(2) int len = progMods.length;

(3) int len = length(progMods);

(4) int len = length.progMods;

18. B

[Please turn the page]


ICT2612
Java 17 Example paper 2014

19. Indicate which of the following commands will correctly sort the array
modules in ascending order.

(1) Arrays.sort(modules, Collections.Order());

(2) Arrays.sort(modules);

(3) modules.sort()

(4) Arrays(modules).sort();

19. B

20. Study the code below and indicate what the value will be of place
after the code executed:

String[] colours = {"black","blue","green","red","yellow"};


int place = Arrays.binarySearch(colours,"blue");

(1) 4
(2) 3
(3) 2
(4) 1

20. D

21. Study the code below and indicate what the value will be for myString
after the code has executed:

char[] data = {i, c, t};


String myString = new String(data);

(1) ict
(2) i c t
(3) i c t

[Please turn the page]


ICT2612
Java 18 Example paper 2014

(4) Error message: error: incompatible types


Reason: you cannot create a new type String from type char[].

21. A

22. The programmer is requested to create code that will create a new
user. When a new user is created the user is requested to provide a
user name and a new password. The new password is entered twice
and compared to ensure that they match.

Note: assume that the following class and function exist:

- the class User(String userName, String userPassword)


- the function requestUserInput(). This function requests the user
to input information and returns a String to the calling program.

Indicate which of the following code will correctly test and create the
new instance of the user.

(1) String uName, uPw1, uPw2, message="";


boolean isEqual = false;
uName = requestUserInput();

uPw1 = "";
uPw2 = "";

while (isEqual <> true) {


uPw1 = requestUserInput();
uPw2 = requestUserInput();

if (uPw1 == uPw2)
isEqual = true;
else
message = ("Passwords not equal");

}//end while

//create new instance of the user


User user1 = new User(uName,uPw1);

[Please turn the page]


ICT2612
Java 19 Example paper 2014

(2) String uName, uPw1, uPw2, message="";


boolean isEqual = false;
uName = requestUserInput();

uPw1 = "";
uPw2 = "";

while !(isEqual) {
uPw1 = requestUserInput();
uPw2 = requestUserInput();

if (uPw1 == uPw2)
isEqual = true;
else
message = ("Passwords not equal");

}//end while

//create new instance of the user


User user1 = new User(uName,uPw1);

(3) String uName, uPw1, uPw2, message="";


boolean isEqual = false;
uName = requestUserInput();

uPw1 = "";
uPw2 = "";

while (isEqual != true) {


uPw1 = requestUserInput();
uPw2 = requestUserInput();

if (uPw1.compareTo(uPw2))
isEqual = true;
else
message = ("Passwords not equal");
}//end while
//create new instance of the user
User user1 = new User(uName,uPw1);

[Please turn the page]


ICT2612
Java 20 Example paper 2014

(4) String uName, uPw1, uPw2, message="";


boolean isEqual = false;
uName = requestUserInput();

uPw1 = "";
uPw2 = "";

while (isEqual != true) {


uPw1 = requestUserInput();
uPw2 = requestUserInput();

if (uPw1.equals(uPw2))
isEqual = true;
else
message = ("Passwords not equal");

}//end while

//create new instance of the user


User user1 = new User(uName,uPw1);

22. D

23. Study the code below and indicate what the value of place will be
after the execution of the code:

String name = "Johnny Mkhenzi";

int place = name.lastIndexOf(n);

(1) 3
(2) 3 4 11
(3) 11
(4) Error message. You cannot determine the value of \texttt{n}
which is of type \texttt{char}, within a value of type \texttt{String}.

23. C

[Please turn the page]


ICT2612
Java 21 Example paper 2014

24. Study the code below and indicate what the value of thisColour will
be after the code is executed.

String colour = "red";


String thisColour = !colour.equals(colour)
? "is red":"is not red";

(1) is red

(2) is not red

(3) is red : is not red

(4) Error message. The code is not correct.

24. A

25. The following errors are all examples of errors.

Error 1:
required: String
found: int

Error 2:
error: ; expected

Error 3:
error: cannot find symbol

(1) logic
(2) syntax
(3) exception
(4) unchecked exception

25. B

[Please turn the page]


ICT2612
Java 22 Example paper 2014

26. The user entered the following code, but received an error message
when trying to execute the code.

int[] marks = {20,45,67,56,43};


int sum = 0;

for (int i=1; i < 10; i++){


sum = sum + marks[i];
}//end for

Indicate which of the following codes the user can use that will inter-
cept the error without crashing the program:

(1) boolean error = false;

for (int i=1; i < 10; i++){


try{sum = sum + marks[i];
}exception (Catch e){error=true;}
}//end for

(2) boolean error = false;

for (int i=1; i < 10; i++){


try{sum = sum + marks[i];
} catch (error = true;)
}//end for

(3) boolean error = false;


for (int i=1; i < 10; i++){
try{sum = sum + marks[i];
}catch (Exception e){error=true;}
}//end for

(4) boolean error = false;

for (int i=1; i < 10; i++){


Exception try{sum = sum + marks[i];
}catch (Exception e){error=true;}
}//end for

[Please turn the page]


ICT2612
Java 23 Example paper 2014

26. C

Study the code below and answer questions 27 to 29.

int correctNum = 76;

int test = get_user_input;

String symbol, message="";


String[] messages = {"You guessed to low",
"You guessed to high","That is correct!"};

if (test < correctNum) {message = messages[1];}


else if (test > correctNum) {message = messages[2];}
else {message = messages[2];}

String output = message;

27. What will the final value of output be if get user input = 80.
(1) You guessed to low

(2) You guessed to high

(3) That is correct!

(4) Error message

27. C

28. Indicate which of the following code can replace the above if..else
... statements and still render the same results.

(1) switch(test){
case.less(correctNum) : {message = messages[0];} break;
case.greater(correctNum) : {message = messages[2];} break;
default: message = messages[1];
}

[Please turn the page]


ICT2612
Java 24 Example paper 2014

(2) switch(test){
case < (int)correctNum : {message = messages[0];} break;
case > (int)correctNum : {message = messages[2];} break;
default: message = messages[1];
}

(3) switch(test){
case < correctNum : {message = messages[0];} break;
case > correctNum : {message = messages[2];} break;
default: message = messages[1];
}

(4) None of the above.

28. D

Study the code below and answer question 29:

int month = 6;
String monthName;

switch (month){
case 1 : monthName = "January"; break;
case 2 : monthName = "February"; break;
case 3 : monthName = "March"; break;
case 4 : monthName = "April"; break;
case 5 : monthName = "May"; break;
case 6 : monthName = "June"; break;
case 7 : monthName = "July"; break;
case 8 : monthName = "August"; break;
case 9 : monthName = "September"; break;
case 10 : monthName = "October"; break;
case 11 : monthName = "November"; break;
default: monthName = "December";
}//end switch

29. Which of the following coding can replace the above switch state-
ment but still render the same results.

[Please turn the page]


ICT2612
Java 25 Example paper 2014

(1) int month = 6;


String[] months = { "","January", "February", "March",
"April","May", "June",
"July","August","September",
"October","November","December"};

monthName = months[month];

(2) int month = 6;


String[] months = {"January", "February", "March", "April",
"May", "June","July","August",
"September","October","November","December"};

monthName = months[month];

(3) int month = 6;


String[] months = [""],["January"], ["February"],
["March"], ["April"],["May"], ["June"],
["July"],["August"],["September"],
["October"],["November"],["December"]};

monthName = months[month-1];

(4) int month = 6;


Months monthName = new Month(month);

29. A

30. Indicate which of the following statements best describe a public


access modifier:

(1) Only allows access from inside the same class.


(2) Allows access inside the class, subclass or other classes of the
same package as the modifier.
(3) Allows access from inside the same package.
(4) Allows access from anywhere, inside and from outside the pack-
age.

[Please turn the page]


ICT2612
Java 26 Example paper 2014

30. D

[Please turn the page]


ICT2612
Java 27 Example paper 2014

Study the code below and answer questions 31 to 35 that follow.

public class Books {

String title, isbn, author;


double price;

static int totalBooks = 0;

public Books()
{
totalBooks++;
}

public Books(String t, String i, String a){


this.title = t;
this.isbn = i;
this.author = a;
totalBooks++;

public void display(){


String show = this.title + " " + this.author + " " + this.isbn;

private String getISBN(){


return isbn.substring(0,3);
}

31. are the fields for the class Books.

(1) String title, isbn, author;


static int totalBooks = 0;

(2) String title, isbn, author;

[Please turn the page]


ICT2612
Java 28 Example paper 2014

(3) String title, isbn, author;


double price;

static int totalBooks = 0;

(4) double price;

31. C

32. The purpose of the static variable totalBooks is to keep track of the
number of instances of Books.
Indicate which of the following commands will correctly call this vari-
able from the main program.

(1) int numBooks = Books.totalBooks;

(2) Books book1 =


new Books("Interactive Programming","John Lexton","5421231");
int numBooks = book1.totalBooks;

(3) Books book1 =


new Books("Interactive Programming","John Lexton","5421231");
book1.display();

(4) Error message. You cannot call a static variable directly from the
main program.

32. A

33. Study the code below and indicate what the value for the variable
show will be after the code is executed:

Books book2 = new Books("Intro to Programming","Len Dickson","7-7897-7845");


book2.author = "Peter Pan";
book2.isbn = "7-777-7777";
book2.display();

[Please turn the page]


ICT2612
Java 29 Example paper 2014

(1) Intro to Programming

(2) Intro to Programming Len Dickson 7-7897-7845

(3) Intro to Programming Peter Pan 7-7897-7845

(4) Intro to Programming Peter Pan 7-777-7777

33. D

34. getISBN() is an example of a method.

(1) public access


(2) restricted
(3) public
(4) private

34. D

35. Study the code below and indicate what the value of show will be after
the code has executed:

Books book3 = new Books();


book3.display();

(1) Error message. You cannot pass empty parameters to the class
Books.
(2) Nothing will be printed since no parameters are passed from the
main program to the class Books.
(3) null null null
(4) Error message. There is a constructor in the class that do al-
low an instance of the class to be created without parameters,
however the program will crash when the getISBN() method is
called.

35. C

[Please turn the page]


ICT2612
Java 30 Example paper 2014

36. The user entered the following code in the main program:

Employee emp1 = new Employee("Karen Botha","IT");


String display = emp1.getUserName();

Indicate which of the following code below will correctly create the
classes with constructors for Employee and UserName.

The value that should be returned to the main program for display
should be something similar to: karenb othaIT 7wherethelast 7 isarandomlygeneratednum

(1) public class Employee {

String name, username, dept;

public Employee(String n, String d){


this.name = n;
this.dept = d;
int number = (int) (Math.random()*10);
UserName un1 = new UserName(name,dept, number);

username = un1.createUserName();
}

public String getUserName()


{
return username;
}

}
--------------------------------------------

public class UserName {


String name, dept, number;

public UserName(String n, String d, int num ){


this.name = n;
this.dept = d;
this.number = String.valueOf(num);

[Please turn the page]


ICT2612
Java 31 Example paper 2014

public String createUserName(){


return this.name.toLowerCase().replace( ,_) + this.dept + this.numbe

}
}

(2) public class Employee {

String name, username, dept;

public Employee(String n, String d){


this.name = n;
this.dept = d;
int number = Math.random()*10;
username = un1.createUserName();
}

public String getUserName()


{
return username;
}

}
--------------------------------------------

public class UserName {


String name, dept, number;

public UserName(String n, String d, int num ){


this.name = n;
this.dept = d;
this.number = num;
}

public String createUserName(){


return this.name.toLowerCase().replace( ,_) + this.dept + this.numbe

[Please turn the page]


ICT2612
Java 32 Example paper 2014

}
}
(3) public class Employee {

String name, username, dept;

public Employee(String n, String d){


this.name = n;
this.dept = d;
int number = (int) (Math.random()*10);
getUserName()
}

public String getUserName()


{
UserName un1 = new UserName(name,dept, number);

username = un1.createUserName();
return username;
}

}
--------------------------------------------

public class UserName {


String name, dept, number;

public UserName(String n, String d, int num ){


this.name = n;
this.dept = d;
this.number = String.valueOf(num);
}

public String createUserName(){


return this.name.toLowerCase().replace( ,_) + this.dept + this.numbe

}
}

[Please turn the page]


ICT2612
Java 33 Example paper 2014

(4) public class Employee {

String name, username, dept;

public Employee(String n, String d){


this.name = n;
this.dept = d;
int number = (int) (Math.random()*10);
getUserName()
}

public String getUserName()


{
UserName un1 = new UserName(name,dept, number);

username = un1.createUserName();
return username;
}

}
--------------------------------------------

public class UserName {


String name, dept, number;
this.number = String.valueOf(num);
}

public String createUserName(){


return this.name.toLowerCase().replace( ,_) + this.dept + this.numbe

}
}

36. C

[Please turn the page]


ICT2612
Java 34 Example paper 2014

Study the code below that is used to create the class Stats with two
constructors. The purpose of this class is to determine the average,
total, highest and lowest of the array data.

Example: the mean (average) of the values 10,15,40 and 88 is 38.28


The methods getAverage(), getTotal(), getValueAtPos(), getLow() and
getHigh() are used to do all the calculations with.
Answer questions 37 to 40 based on the class Stats.

import java.util.Arrays;

public class Stats {

int myData[] = new int[20];


int position;

public Stats(int[] data) {


this.myData = data;
}

public Stats(int[] data, int pos) {


this.myData = data;
this.position = pos;

public int getValueAtPos(){


return this.myData[this.position];
}

public double getAverage(){

return getTotal() / this.myData.length;


}

private int getTotal(){


int total = 0;
for (int i = 0; i < this.myData.length; i++)

[Please turn the page]


ICT2612
Java 35 Example paper 2014

total = total + this.myData[i];


return total;
}

public int getLow() {


Arrays.sort(myData);
return myData[0];
}

public int getHigh(){


Arrays.sort(myData);
return myData[myData.length-1];
}

37. The constructors in the class Stats are:

(1) public Stats(int[] data) {....


public Stats(int[] data, int pos){.....

(2) public int getValueAtPos(){....


public double getAverage(){....
public int getTotal(){....
public int getLow(){....
public int getHigh(){....

(3) public Stats(int[] data) {....


public Stats(int[] data, int pos){.....

public int getValueAtPos(){....


public double getAverage(){....
public int getTotal(){....
public int getLow(){....
public int getHigh(){....

[Please turn the page]


ICT2612
Java 36 Example paper 2014

(4) Stats stat1 = new Stats(data);


Stats stat2 = new Stats(data,3);

37. A

38. What will the result of display be if the following instance of Stats is
created.

Stats stat1 = new Stats(data,3);


int display = stat1.getValueAtPos();

(1) 67

(2) 67.0

(3) 34

(4) 34.0

38. C

39. What will the result of display2 be if the following instance of Stats
is created.

Stats stat2 = new Stats(data);


int display2 = stat2.getValueAtPos();

(1) Error message. No parameter passed to the constructor.

(2) 90

(3) 90.0

(4) 78.0

39. C

[Please turn the page]


ICT2612
Java 37 Example paper 2014

40. Study the code below and indicate what the result will be for total:

Stats stat3 = new Stats(data);


int total = stat3.getTotal();

(1) Error message. The switch statement has an error. getTotal() is


a private method in the class Stats.
(2) 437
Reason: The method getTotal() adds all the values for the array
and returns an integer value to the calling program.
(3) 437

Reason: The method getTotal() adds all the values for the array
and returns a double value to the calling program.
(4) Error message. The method \texttt{getTotal()} uses the \texttt{this.myDa
this value is not initialised in the method and will render an error mes

40. A

[Please turn the page]


ICT2612
Java 38 Example paper 2014

41. Study the code below and indicate what the result will be:

Stats stat4 = new Stats(data);


double avg = stat4.getAverage();

(1) Error message. The getAverage() method is calling a private


method, getTotal() in the class Stats.
(2) 1.0
Error message. The getAverage() method is referring to the
function this.myData.length but there is no such function for
arrays.
(3) Error message. The \texttt{getAverage()} method is calculating a value
whilst returning a value of type doulbe.

(4) No error message. The average will be calculated and returned to the ca

41. B

42. A(n) approach to programming results in programs that are easier


to develop, debug and maintain.

(1) structured
(2) procedural
(3) functional
(4) object-oriented

42. D

43. A(n) is a small program that can be downloaded and executed as


part of a displayed Web page.

(1) object
(2) method
(3) servlet
(4) application (app)

43. D

[Please turn the page]


ICT2612
Java 39 Example paper 2014

44. The of a class are instructions that the class uses to manipulate
values, generate output, or perform actions.

(1) methods
(2) attributes
(3) objects
(4) classes

44. A

45. In object-oriented terminology, the characteristics of an object are


defined by its .

(1) variables
(2) triggers
(3) attributes
(4) instances

45. C

46. All of the following are conceptual constructs of object-oriented pro-


gramming except .

(1) iteration
(2) encapsulation
(3) inheritance
(4) polymorphism

46. A

47. is the capability of an object to have data and functionality avail-


able to the user, without the user having to understand the imple-
mentation within the object.

(1) aggregation
(2) inheritance
(3) polymorphism

[Please turn the page]


ICT2612
Java 40 Example paper 2014

(4) encapsulation

47. D

48. means that a programmer can reuse a class, along with its meth-
ods and data, to create a subclass, which saves time and coding
(1) aggregation
(2) encapsulation
(3) inheritance
(4) polymorphism

48. C

49. allows an instruction to be given to an object using a generalized


rather than a specific, detailed command.

(1) aggregation
(2) inheritance
(3) polymorphism
(4) encapsulation

49. C

50. In a Java program the folder includes the resource files (images,
music and videos) for the project:

(1) assets
(2) bin
(3) libs
(4) res

50. D


UNISA 2014

[TOTAL: 50]

[Please turn the page]


ICT2612
Java 41 Example paper 2014

Todo list
Java: Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1. C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
2. A . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
3. C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
4. D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
5. D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
6. C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
7. B . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
8. B . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
9. B . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
10. C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
11. B . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
12. C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
13. C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
14. B . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
15. A . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
16. B . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
17. E . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
18. B . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
19. B . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
20. D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
21. A . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
22. D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
23. C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
24. A . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
25. B . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
26. C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
27. C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
28. D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24

[Please turn the page]


ICT2612
Java 42 Example paper 2014

29. A . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
30. D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
31. C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
32. A . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
33. D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
34. D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
35. C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
36. C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
37. A . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
38. C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
39. C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
40. A . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
41. B . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
42. D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
43. D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
44. A . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
45. C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
46. A . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
47. D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
48. C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
49. C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
50. D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40

[Please turn the page]

S-ar putea să vă placă și