ICT2612/103/1/2021
Tutorial Letter 103/1/2021
Interactive Programming
ICT2612
Semester 1
School of Computing
IMPORTANT INFORMATION
This tutorial letter contains important information
about your Assignment 2.
DUE DATE: 09 April 2021
UNIQUE CODE: 585827
INSTRUCTIONS
Work through all the questions and select the correct answer. When you are done, logon to
myUnisa. Select assignment 2 and complete and submit the online MCQ
ICT2612/103/1/2021
1. What will be the value of str after the following code execute?
String str = "7" + 5 + 10
(1) 7510
(2) 22
(3) 25
(4) 10
(5) None of the above
2. To prevent any method from overriding, we declare the method as...
(1) static
(2) const
(3) final
(4) abstract
(5) public
Rectangle r1 = new Rectangle();
r1.setColor(Color.blue);
Rectangle r2 = r1;
r2.setColor(Color.red);
3. After the above piece of code is executed, what are the colors of r1 and r2 (in this order)?
(1) Color.blue, Color.red
(2) Color.blue, Color.blue
(3) Color.red, Color.red
(4) Color.red, Color.blue
(5) None of the above.
4. What is the type and value of the following expression? (Notice the integer division)
-4 + 1/2 + 2*-3 + 5.0
(1) int -5
(2) double -4.5
(3) int -4
(4) double -5.0
(5) None of the above.
ICT2612/103/1/2021
5. What is the value of a and s after compiling the below code
public static int foo(int a, String s) {
s = "Yellow";
a = a + 2;
return a;
}
public static void bar() {
int a = 3;
String s = "Blue";
a = foo(a, s);
System.out.println("a=" + a + " s=" + s);
}
public static void main(String[] args) {
bar();
}
(1) a = 3 s = Blue
(2) a = 5 s = Yellow
(3) a = 3 s = Yellow
(4) a = 5 s = Blue
(5) none of the above.
6. Which of the following variable declaration would NOT compile in a java program?
(1) int var;
(2) int VAR;
(3) int var1;
(4) int var_1;
(5) int 1_var;
Consider the following class definition:
public class MyClass{
private int value;
public void setValue(int i){ / code / }
// Other methods…
}
ICT2612/103/1/2021
7. The method setValue assigns the value of i to the instance field value. What could you write
for the implementation of setValue?
(1) value = i;
(2) this.value = i;
(3) value == i;
(4) Both (A) and (B) above
(5) (A), (B) and (C) above
8. Which of the following is TRUE?
(1) In java, an instance field declared public generates a
compilation error.
(2) int is the name of a class available in the package
java.lang
(3) Instance variable names may only contain letters and digits.
(4) A class always has a constructor (possibly automatically
supplied by the java compiler).
(5) The more comments in a program, the faster the program runs.
9. Which of the following is true about the constructor?
(1) Must have the same name as the class it is declared within.
(2) Is used to create objects.
(3) May be declared private
(4) Both (A) and (B) above
(5) (a), (b) and (c) above.
Consider the following code and answer the following question,
ublic class MyClass{
public MyClass(){/code/}
// more code…
}
10. To instantiate MyClass, you would write?
(1) MyClass mc = new MyClass();
(2) MyClass mc = MyClass();
(3) MyClass mc = MyClass;
(4) MyClass mc = new MyClass;
(5) The constructor of MyClass should be defined as, public void
MyClass(){/code/}.
ICT2612/103/1/2021
11. What is a byte code?
(1) The type of code generated by a Java compiler.
(2) The type of code generated by a Java Virtual Machine.
(3) It is another name for a Java source file.
(4) It is the code written within the instance methods of a
class.
(5) It is another name for comments written within a program.
12. Which of the following statements about Java Threads is correct?
(1) Java threads don’t allow parts of a program to be executed
in parallel
(2) Java is a single-threaded language
(3) Java’s garbage collector runs as a high priority thread
(4) Ready, running and sleeping are three states that a thread
can be in during its life cycle
(5) Every java application is not multithreaded.
Consider the following Java Program:
public class Computation {
public static void main(String args[]) {
int result, x;
x = 1;
result = 0;
while (x <= 10) {
if (x % 2 == 0) result += x;
++x;
}
System.out.println(result);
}
}
13. Which of the following will be the output of the above program?
(1) 55
(2) 30
(3) 25
(4) 35
(5) 45
ICT2612/103/1/2021
Consider the following statement(s) about Java:
I. All white-space characters (blanks) are ignored by the
compiler.
II. Java keywords can be used as variable names.
III. An identifier does not begin with a digit and does not
contain any spaces.
IV. The execution of Java applications begins at method main.
14. Which of them is correct?
(1) Both (I) and (III) above
(2) Both (II) and (IV) above
(3) Both (I) and (II) above
(4) (III) and (IV) above
(5) All (I), (II), (III) and (IV) above.
15. In object-oriented programming, the process by which one object acquires the properties of
another object is called
(1) Encapsulation
(2) Polymorphism
(3) Overloading
(4) Inheritance
(5) Overriding.
16. What will be the output of the following program
public class Print {
public static void main(String[] args) {
int c = 4
System.out.println(c = c++);
}
}
(1) 2
(2) 4
(3) 5
(4) 8
(5) Syntax error.
ICT2612/103/1/2021
17. Study the code below and indicate what the value of i_total will be.
double d_total = 123.5;
int i_total = (int)d_total;
(1) 123
(2) 124
(3) 123.0
(4) 123.5
(5) error message
18. Indicate which of the following declarations will render an error?
(1) boolean result = true;
(2) int result = 10.0;
(3) int result = 'a';
(4) float result = 1.23;
(5) long result = 123_456_789;
19. Study the code below and indicate what the values of answer will be:
int x = 5, y = 6, z = 0, k = 2;
int answer = (x + y) * z / k;
(1) 0
(2) 0.0
(3) 5
(4) 5.0
(5) Division by zero error message.
ICT2612/103/1/2021
20. Study the code below and indicate what the values will be for hasMore, total and place.
boolean hasMore = true;
int[] numbers = {1,2,4,3,2,1};
int place = numbers.length - 1, total = 0;
while (hasMore)
total = total + numbers[place];
place--;
if (place == 0) hasMore = false;
(1) total: 12 place: 0 hasMore: false
(2) total: 11 place: 0 hasMore: false
(3) total: 11 place: 0 hasMore: true
(4) total: 12 place: 1 hasMore: false
(5) total: 12 place: 1 hasMore: true
Study the incomplete code below that is used to create the class Book. Answer questions 21 to 26
that follow.
public class Car {
//instance variables
String brand, fueltype, manual_auto;
double price;
//constructors
public Car(String b, String f, String m, float p){
//first constructor
// (i)
ICT2612/103/1/2021
numCars++;
public Car(String brand, String fueltype, float price){
//second constructor
this.brand = brand;
this.fueltype = fueltype;
this.price = sell_price_special(price);
numCars++;
private float sell_price(float p){
return (float)(p * 1.10);
}//sell_price
private double sell_price_special(float p){
//(ii)
}//sell_price_special
public String display(){
String show = this.brand + "R " + this.price;
return show;
}//display
}//Car
21. Indicate which of the following will correctly link the variables for the first constructor (i) to the
instance variables.
(1) this.b = b;
this.f = f;
this.m = m;
this.p = sell_price(p);
(2) this.b = brand;
this.f = fueltype;
ICT2612/103/1/2021
this.m = manual_auto;
this.p = sell_price(p);
(3) this.brand = b;
this.fueltype = f;
this.manual_auto = m;
this.price = sell_price(p);
(4) String brand = b;
String fueltype = f;
String manual_auto = m;
float price = sell_price(p);
(5) String b = b;
String f = f;
String m = m;
float p = sell_price(p);
22. Indicate which of the following code (ii) will correctly calculate a discount of 10% and
return the new discount price in the sell_price_special() method
(1) float discount, newprice;
discount = (float) (p * 0.10);
newprice = p / discount;
return newprice;
(2) double discount, newprice;
discount = (float) (p * 0.10);
newprice = p - discount;
return newprice;
(3) double discount, newprice;
ICT2612/103/1/2021
discount = p / 1.10;
newprice = p - discount;
return newprice;
(4) double discount, newprice;
discount = p / 1.10;
newprice = discount;
return newprice;
(5) double discount, newprice;
discount = p / 1.10;
newprice = discount;
return double(newprice);
23. Indicate which of the following will correctly create two new instances (car1 and car2) of the
class Car.
(1) Car car1 = new Car("mazda","petrol",150000);
Car car2 = new Car("nissan","diesel","manual",250000);
(2) Car = car();
car1 = new Car("mazda","petrol",150000.00);
car2 = new Car("nissan","diesel","manual",250000.00);
(3) car1 = new Car("mazda","petrol",150000);
car2 = new Car("nissan","diesel","manual",250000);
(4) Car car1 = new Car(brand="mazda",fueltype="petrol",price=150000);
Car car2 = new Car(b="nissan",f="diesel",m="manual",p=250000);
(5) Car car1 = new
Car(brand="mazda",fueltype="petrol",price=150000.00);
Car car2 = new Car(b="nissan",f="diesel",m="manual",p=250000.00);
ICT2612/103/1/2021
24. Indicate what the value of display will be:
Car car3 = new Car("toyota","diesel",170000,"red");
String display = car3.display();
(1) toyota R 153000.0
(2) toyota R 187000.0
(3) toyota R 153000.0 red
(4) Error message.
The actual and formal argument list differ in length.
(5) Error message.
The constructor Car(String,String,int,String) is undefined.
25. The purpose of the static variable numCars is to keep track of the number of instances of Car.
Indicate which of the following commands will correctly call this variable from the main program.
(1) Error message. You cannot call a static variable directly
from the main program.
(2) int numCars = Car.numCars;
(3) int numCars = Car1.numCars;
(4) int numCars = Car1.display(numCars);
(5) int numCars = Car.display(numCars);
26. Indicate the value of message.
Car car1 = new Car("mazda","petrol",150000);
String message = car1.getFuelType();
ICT2612/103/1/2021
(1) Error message. You cannot call the method getFuelType() from the
main program.
(2) mazda R 135000.0 petrol
(3) mazda R 150000.0 petrol
(4) petrol mazda R 135000.0
(5) Petrol
Assume that the package Arrays is imported.
Study the code below and answer questions 27 to 31 that follow:
//declare arrays and allocate values to the arrays
String[] fueltypes = {"petrol","hybrid","electric","diesel"};
String[] transmission = {"manual","automatic"};
String[] drive = {"2WD","4WD","AWD"};
String[] brands = {"Mazda","Nissan","BMW","Toyota"};
char[] doors = {'2','3','4','5'};
String myDoors = new String(doors);
27. Indicate which of the following statements can replace the array declaration that allocates the
values to the array brands.
(1) String[] brands = new String[4];
brands[0] = "Mazda";
brands[1] = "Nissan";
brands[2] = "BMW";
brands[3] = "Toyota";
(2) String[4] brands;
brands[0] = "Mazda";
brands[1] = "Nissan";
brands[2] = "BMW";
brands[3] = "Toyota";
ICT2612/103/1/2021
(3) String[] brands[4];
brands[0] = "Mazda";
brands[1] = "Nissan";
brands[2] = "BMW";
brands[3] = "Toyota";
(4) String[] brands[4];
brands[0] = new brands{"Mazda"};
brands[1] = new brands{"Nissan"};
brands[2] = new brands{"BMW"};
brands[3] = new brands{"Toyota"};
(5) String[] brands;
brands[0] = new String{"Mazda"};
brands[1] = new String{"Nissan"};
brands[2] = new String{"BMW"};
brands[3] = new String{"Toyota"};
28. Indicate which of the following code will correctly combine the array brands and the array
fueltypes and initialise the array cars.
(1) for (int i = 0; i < brands.length-1; i++){
car[i] = brands[i] + " : " + fueltypes[i];
(2) for (int i = 1; i < brands.length-1; i++){
car[i] = brands[i] + " : " + fueltypes[i];
(3) for (int i = 0; i < brands.length; i++){
car[i] = brands[i] + " : " + fueltypes[i];
}
ICT2612/103/1/2021
(4) for (int i = 0; i < brands.length+1; i++){
car[i] = brands[i] + " : " + fueltypes[i];
(5) for (int i = 0; i < brands.length-1; i++){
for (int j = i; j < brands.length; j++)
car[i] = brands[i] + " : " + fueltypes[j];
29. Indicate the value of myDoors.
(1) Error message: incompatible types.
Reason: you cannot create a new type String from type char[]
(2) 2345
(3) 2 3 4 5
(4) ['2','3','4','5']
(5) [2345]
30. Indicate which of the following commands will correctly sort the array drive in ascending
order.
(1) Arrays.sort(drive,Collections Order());
(2) Arrays.sort(drive);
(3) drive.sort();
(4) drive = drive.sort();
(5) Arrays(drive).sort();
ICT2612/103/1/2021
31. Indicate which one of the following statements will correctly calculate the length of the array
drive.
(1) int len = drive.length();
(2) int len = drive.len();
(3) int len = length(drive);
(4) int len = drive.length;
(5) int len = length.drive;
32. Study the code and indicate the value of place.
String id = "640423";
int place = id.indexOf("4");
(1) 1
(2) 2
(3) 1 3
(4) 2 4
(5) 24
33. Which one of the following is NOT an example of an exception error that can occur in Java?
(1) A user entered invalid data.
(2) A file that needs to be opened cannot be found.
(3) A network connection has been lost in the middle of
communications.
(4) The JVM has run out of memory.
(5) The programmer entered the incorrect code to create a button.
ICT2612/103/1/2021
When entering the code below, the programmer receives an error
message.
int[] daysInMonth = {31,28,31,30,31,30};
int totalDays = 0;
for (int i=0; i < 12; i++){
totalDays = totalDays + daysInMonth[i];
34. Indicate which one of the following options will intercept the error without crashing the
program.
(1) for (int i=0; i < 12; i++){
try{
totalDays = totalDays + daysInMonth[i];
} catch (Exception e){boolean error = true;}
(2) for (int i=0; i < 12; i++){
try{
totalDays = totalDays + daysInMonth[i];
} (catch e){boolean error = true;}
(3) for (int i=0; i < 12; i++){
try{
totalDays = totalDays + daysInMonth[i];
} exception (Catch e){boolean error = true;}
(4) for (int i=0; i < 12; i++){
try{
totalDays = totalDays + daysInMonth[i];
} catch (Exception e)
}
ICT2612/103/1/2021
(5) for (int i=1; i < 12; i++){
try{
totalDays = totalDays + daysInMonth[i];
} catch e {boolean error = true;}
35. Which one of the following is NOT an access modifier in Java?
(1) private
(2) protected
(3) public
(4) void
Consider the three lines of code below.
String s = "30564.5"; //line 1
char[] cArray = s.toCharArray(); //line 2
int answer = cArray.length; //line 3
36. Which one of the following options is correct regarding the above three
statements?
(1) No compiler error and the value of answer is 7.
(2) Compiler errors in line 1 and line 2.
(3) No compiler error and the value of answer is 6.
(4) Compiler error in line 1.
(5) Syntax error in line 2.
37. Indicate the value of result:
int val1 = 11;
int val2 = 2;
double result = val1 % val2;
(1) Error message.
Reason: There is no such operator as %.
ICT2612/103/1/2021
(2) Error message.
Reason: you cannot assign the result of % to a
double value.
(3) 1.0
Reason: the remainder of 11 divided by 2 is 1.
(4) 5
Reason: 11 divided by 2 is 5.
(5) 5.0
Reason: 11 divided by 2 is 5.0, displayed as type
double.
38. Study the code below and indicate the value of place.
String email = "[email protected]";
int place = email.indexOf("@");
(1) Error message. You should search for '@'
(2) 8
(3) 9
(4) 10
(5) @
Study the code below and answer the question that follows.
public class Compare {
static int larger(int x, int y){
if (x > y) return x;
else return y;
static char larger(char x, char y){
if (x > y) return x;
else return y;
ICT2612/103/1/2021
static double larger(double x, double y){
if (x > y) return x;
else return y;
static String larger(String x, String y){
if (x.length() > y.length()) return x;
return y;
public class Main {
public static void main(String[] args) {
Compare c = new Compare();
int s1 = c.larger(10,20);
char s2 = c.larger('a', 'b');
String s3 = c.larger("Hope", "hope");
double s4 = c.larger(10.1, 11);
}
ICT2612/103/1/2021
39. Which one of the following is correct regarding the above code?
(1) Java does not allow multiple methods with the same name in
a class.
(2) The values for s1,s2,s3 and s4 are:
s1: 20
s2: b
s3: hope
s4: 11.0
(3) The values for s1,s2,s3 and s4 are:
s1: 20
s2: b
s3: Hope
s4: 11.0
(4) The values for s1,s2,s3 and s4 are:
s1: 20
s2: b
s3: Hope
s4: 11
(5) The statement
double s4 = c.larger(10.1, 11);
gives a compiler error because there is no function that
matches the
arguments (double, int).
ICT2612/103/1/2021
40. Study the incomplete code below used by a rental car agency.
boolean license = getLicense();
int age = getAge();
String MF = getMF();
boolean fines = getFines();
boolean valid = false;
// (i)
In the agency the rules are enforced when a person apply to rent a car:
- The person must have a valid driver’s license
- A female applicant must be 23 years and older
- A male applicant must be 25 years and older
- The person may not have any outstanding traffic fines
Which one of the following expression will NOT enforce the rules above?
Assume that all the variables used in the options are declared and initialised correctly.
(1) valid = ((license==true) && (fines==false) &&
(((age>=23) && (MF=="F" ))||( (age>=25) && (MF == "M"))));
(2) valid = ((license) && !(fines) &&
(((age>=23) && (MF=="F" ))||( (age>=25) && (MF == "M"))));
(3) valid = ((license==true) && (fines==false) &&
(((age>=23) && (MF.equals("F") ))||
( (age>=25) && (MF.equals("M")))));
(4) valid = ((license) && !(fines) &&
(((age>=23) && (MF.equals("F") ))||
( (age>=25) && (MF.equals("M")))));
(5) valid = ((license=true) && (fines=false) &&
(((age>=23) && (MF="F" ))||( (age>=25) && (MF = "M"))));
ICT2612/103/1/2021
41. Which one of the following statements explains the contents of activity_main.xml file in an
Android application project?
(1) It contains information about the sound and image files
used in the application.
(2) It contains information about the default Graphical User
Interface of the application.
(3) It contains the code of the class MainActivity.
(4) It contains a summary of all the setup information of the
application.
(5) It contains a summary of all the XML files used in the
application.
42. Choose the correct data types for the variables in the code below:
height = 165;
weight = 70.3;
bmi = weight / height / height * 10000;
(1) float height = 165;
float weight = 70.3;
float bmi = weight / height / height * 10000;
(2) double height = 165;
double weight = 70.3;
float bmi = weight / height / height * 10000;
(3) float height = 165;
double weight = 70.3;
float bmi = weight / height / height * 10000;
(4) float height = 165;
float weight = 70.3;
double bmi = weight / height / height * 10000;
ICT2612/103/1/2021
(5) float height = 165;
double weight = 70.3;
double bmi = weight / height / height * 10000;
43. A _____variable, that is not accessible by any other method, class or program is defined
inside a method, constructor or block and will be destroyed when the method has completed.
(1) local
(2) instance
(3) class
(4) open
(5) close
44. The _____ of a class are instructions that the class uses to manipulate values, generate output
or perform various actions.
(1) attributes
(2) classes
(3) overloading
(4) methods
(5) objects
45. In object-oriented terminology, the characteristics of an object are defined by its ___
(1) attributes
(2) classes
(3) instances
(4) triggers
(5) Variables
ICT2612/103/1/2021
46. Which is the correct keyword that indicates inheritance between two classes in Java?
(1) attributes
(2) enlarge
(3) extends
(4) inherits
(5) super
47. The ____ package of Java provides classes for performing arithmetic operations.
(1) java.io
(2) java.calc
(3) java.lang
(4) java.math
(5) java.util
48. The data type Boolean can store data upto ___ that store(s) true / false flags.
(1) 1 bit
(2) 2 bits
(3) 1 byte (8 bits)
(4) 2 bytes
(5) 4 bytes
49. Indicate which one of the following best describes 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
package.
(5) None of the above.
ICT2612/103/1/2021
50. Which one of the following statements describes inheritance correctly?
(1) It allows generic code to be placed in a superclass and
more specialized code in subclasses, thus promoting code
reuse.
(2) It is used when a subclass is more general than a
superclass thereby creating a class hierarchy.
(3) The primary reason for using it is to reduce execution
times of programs.
(4) Using it reduces errors because you can simply copy and
paste code from a superclass to new subclasses.
(5) The primary reason for using it is to make the code more
“human” readable.
TOTAL: 50
2021