2017 Final Exam 2 Flashcards

1
Q

public class Student{
//counts number of student object created
private static int counter = 0;
private int studentID;
private String firstName;
private String lastName;
private int age;
private int[] marks;

public Student(String fname, String lname){
	counter++;
	studentID = counter;
	//some code goes here...
} // some code goes here... 				

//The method below computes the average of all the marks in the student
//array
public double computeAverage(){
//some code goes here…
}
//some code goes here…
}
public class TestStudent{

public static main(String[] args){
	//some java code goes here…
} }
A

a) Initialize first name and last name in the constructor [2]

  • this.fname = fname;
  • this lname = lname;

b) Write a setter method for age data field [2]
public void setAge(int age){
this.age=age;
}

c) Write a setter method that initializes marks data field of type int[] with an array that exists elsewhere [3]

d)Write getter methods for age, first name and last name [3]

e)Write the code that computes the average of marks in the student array[4]

f)Write a method named findHighest, for finding the highest student mark in the array of marks. [4]

g) Conceptually, what is the difference between counter and studentID data fields?

By referring to the Tester class, answer the following;

h) Write code snippet that creates a student object and initializes first name, last name and age [3]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

a) Explain the importance of a constructor in the java program. [2]

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

b) There are two forms of inheritance relationships: Has-A and IS-A
Differentiate the two in the following manner;
i. By definition [4]

ii. By using an example of a java code of three class: Animal, Dog and Feet. [6]

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

c) Consider the class below and answer the following questions;
public class MyClass{

//some code goes here…

//given a string and a number, this method finds the 
//the character in a string, whose index is indicated 
 //by number

public static char findChar (String word, int number){ 
	//some code here...
} }
A

(i) Using one java instruction, show how you would call method, findChar, in a different class [3]
char myChar =MyClass.findChar(“Thursday”, 5);

1 mark for using assignment statement
1 mark for using class name when calling the method
1 mark for a declared char variable that receives returned string

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

b) What is the difference between a constructor and a method? [2]

A

Constructors initialize objects when they are created using the new keyword. They have no return type and share the same name as the class.

Methods define actions or behaviors that objects can perform. They can have a return type (like void or int) and any name that follows naming conventions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

c) What is the purpose of garbage collection in Java, and when is it used? [3]

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

d) What is static in java? [2]

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

e) What is an infinite loop? [2]

A

It is a loop that never comes to an end when it is being executed

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

f) Write the code for an infinite loop. [3]

A

while(true){
System.out.println(“infinite loop”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

g) What are the three things which a programmer must consider when using a loop that should terminate at some point?

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

h) Which loop should you use in a situation where you want the loop to repeat until the Boolean expression is false, but the loop should execute at least once? [1]

A

Do-while

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

i) Why should you indent the statements in the body of a loop? [2]

A

To make the code readable

To differentiate the code that belong to the loop and the one that does not

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

i.In the Java programming language, all source code is first written in plain text files ending with the ______extension. [1]

A

.java

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

ii. The java compiler generates ___________ [1]

A

Machine code

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Describe a valid assignment statements. [1]

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

iv. What is a name given to a storage location in a computer’s memory [1]

A

variable

17
Q

v. State java keyword used to declare a named constant

A

final

18
Q

vi. State one primitive data type

A

char, byte, float, double, boolean, int

19
Q

vii. Choose the appropriate data type for this value: true

A

boolean

20
Q

choose the appropriate data type for this value: “volatile”

A

“String”

21
Q

x. Choose the appropriate data type for this value: female

A

char

22
Q

i. Methods that are marked protected can be called in any subclass of that class

A

true

23
Q

ii. The return value from a method must always match the declared return type.

A

true

24
Q

iii. A static method can refer to any instance variable of the class

A

false

25
Q

iv. A method in a class declared as static can only access static class members.

A

true

26
Q

v. Array indexing always starts with the number zero

A

true

27
Q

vi. Mathematicians and computers interpret the equal sign (=) in the same way.

A

false

28
Q

vii. Assignment operator is evaluated Left to Right.

A

false

29
Q

viii. In an instance method or a constructor, “this” is a reference to the current object.

A

true

30
Q

a) Explain the main differences between superclass and subclass. [4]

A

a Superclass (Parent Class)Doesn’t inherit from any other class (except for the root
Object class in Java).

general class that defines a set of attributes and methods

while

Inherits from a superclass and can add its own specific attributes and methods.

a subclassUses the extends keyword to specify the superclass.

31
Q

c) Distinguish method overriding from method overloading

A

Method overriding happens between
classes with an inheritance relationship. It uses the same method signature (name and parameters) in subclass to provide a specialized implementation.

Method overloading occurs within a single class. It has multiple methods with the same name but different parameter lists. This allows handling different data types or functionalities.

32
Q

d) Give TWO (2) types of methods that cannot be overridden

A

static, private, and final methods cannot be overridden

33
Q

e) Distinguish super class from sub class

A

A super class is a parent class. A sub class is an extended version of the parent class. Sub class is also known as child class because it came from the parent class

34
Q

f) Name the class which is at the highest level of class hierarchy in java

A

object class

35
Q

What is the difference between the following, concentually [2]
Scanner sc1 = new Scanner(System.in);

Scanner sc2 = new Scanner(new File(“numbers.txt”));

A

sc1 is a Scanner object that reads from the keyboard

whereas sc2 is another Scanner object but it reads from a file

36
Q

a) Distinguish between PRIVATE and PROTECTED access specifiers in terms of accessing data fields in the superclass from a subclass.

A

Private data fields cannot be accessed from derived classes but protected data fields can be accessed from derived classes