Documente Academic
Documente Profesional
Documente Cultură
GENERAL INSTRUCTIONS
1
ICT2612
Java 2 Example paper 2014
1. Study the code below and indicate what the value of mark will be:
(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
(1) Line 1
(2) Line 2
(3) Line 3
(4) Line 4
(5) Line 5
2. A
3. Study the code below and indicate what will the result of d answer
will be:
(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.
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;
int k = 2;
answer = (x + y) * z / k;
(1) 14.5
(2) 22
(3) 17
(4) 17.0
5. D
Indicate which of the code below is the correct code for the above
formula:
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.
----------------------------------------------
String message="";
for (int i=0; i < Student.numStudents; i++)
message = message + student[i].getStudentName()+"\n";
(4) 103
7. B
8. B
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.
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++;
}
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".
10. C
11. Study the code below and indicate what the values will be for hasMore,
total and place after the program has executed.
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
(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.
//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);
//(...ii...)
}//sell_price
}//Car
12. (i): Indicate which of the following will correctly link the variables for
the first constructor to the instance variables.
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.
13. C
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:
(5) Error message: The actual and formal argument list differ
in length.
15. A
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.
16. B
17. Indicate which of the following code will correctly combine the two
arrays modules and levels and initialise the array progMods.
(4)
for (int i=0; i<4; 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.
18. B
19. Indicate which of the following commands will correctly sort the array
modules in ascending 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:
(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:
(1) ict
(2) i c t
(3) i c t
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.
Indicate which of the following code will correctly test and create the
new instance of the user.
uPw1 = "";
uPw2 = "";
if (uPw1 == uPw2)
isEqual = true;
else
message = ("Passwords not equal");
}//end while
uPw1 = "";
uPw2 = "";
while !(isEqual) {
uPw1 = requestUserInput();
uPw2 = requestUserInput();
if (uPw1 == uPw2)
isEqual = true;
else
message = ("Passwords not equal");
}//end while
uPw1 = "";
uPw2 = "";
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);
uPw1 = "";
uPw2 = "";
if (uPw1.equals(uPw2))
isEqual = true;
else
message = ("Passwords not equal");
}//end while
22. D
23. Study the code below and indicate what the value of place will be
after the execution of the code:
(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
24. Study the code below and indicate what the value of thisColour will
be after the code is executed.
(1) is red
24. A
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
26. The user entered the following code, but received an error message
when trying to execute the code.
Indicate which of the following codes the user can use that will inter-
cept the error without crashing the program:
26. C
27. What will the final value of output be if get user input = 80.
(1) You guessed to low
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];
}
(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];
}
28. D
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.
monthName = months[month];
monthName = months[month];
monthName = months[month-1];
29. A
30. D
public Books()
{
totalBooks++;
}
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.
(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:
33. D
34. D
35. Study the code below and indicate what the value of show will be after
the code has executed:
(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
36. The user entered the following code in the main program:
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
username = un1.createUserName();
}
}
--------------------------------------------
}
}
}
--------------------------------------------
}
}
(3) public class Employee {
username = un1.createUserName();
return username;
}
}
--------------------------------------------
}
}
username = un1.createUserName();
return username;
}
}
--------------------------------------------
}
}
36. C
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.
import java.util.Arrays;
37. A
38. What will the result of display be if the following instance of Stats is
created.
(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.
(2) 90
(3) 90.0
(4) 78.0
39. C
40. Study the code below and indicate what the result will be for total:
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
41. Study the code below and indicate what the result will be:
(4) No error message. The average will be calculated and returned to the ca
41. B
(1) structured
(2) procedural
(3) functional
(4) object-oriented
42. D
(1) object
(2) method
(3) servlet
(4) application (app)
43. D
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
(1) variables
(2) triggers
(3) attributes
(4) instances
45. C
(1) iteration
(2) encapsulation
(3) inheritance
(4) polymorphism
46. A
(1) aggregation
(2) inheritance
(3) polymorphism
(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
(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]
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
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