Java Flashcards

1
Q

Which of the following are valid Java types?

short
int
byte
double

A

short, int, double

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

Which of the following are valid Java assignments?

1) boolean success = “Yes”;

2) char oneLetter = ‘a’;

3) String welcomeMessage = “Hello Stranger”;

4) float discount = 1.5;

A

2, 3

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

Which of the following Java constructs could (sensibly) be used to store 5 different string values?

1) String[] strings = new String[5];

2) Set<String> strings = new HashSet<String>();</String></String>

3) Integer string = new Integer(5);

4) List<String> strings = new LinkedList<String>();</String></String>

A

1, 4

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

Which of the following is true for a Java class?

1) May contain attributes and methods

2) Can be abstract or concrete

3) Can inherit from multiple other classes

4) Can be instantiated, which creates an object

A

1, 2, 4

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

Which of the following is true for SVN?
1) Is a distributed version control system
2) Supports only one user per server
3) Can be used via command line or through other tools
4) Is the only version control system in existence

A

1, 3

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

Which of the following are valid primitive Java types?
int
single
double
large

A

int, double

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

Which of the following are valid kinds of Java statements (covered in the lecture)?
1) If Statement: if (…) { … }
2) During Statement: during ( … ) { … }
3) Repeat-Until Statement: repeat { … } until ( … )
4) For Statement: for (… ; … ; …) { … }

A

1, 4

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

Write a for statement. Loop in range(10)

A

for (int i = 0; i < 10; i++) {
System.out.println(i);
}

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

Which of the following is true for a Java class?
1) Can contain constructors, attributes and methods
2) Must be declared static to work properly
3) Within the class, this refers (among others) to the current instance of the class
4) Can extend multiple other classes

A

1, 3

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

Which of the following is true for overloading in Java?
1) Replaces a method of a base class with a new implementation
2) Must use the Java keyword overload
3) Overloading method must define different parameters from overloaded method(s)
4) Is not supported in Java but only in languages such as C++

A

3

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

Which of the following is true for inheritance in Java?
1) Is possible only for abstract classes
2) When a class A inherits from a class B then B is called the superclass of A
3) Causes that the inheriting class has access to all attributes and methods of the superclass
4) Objects of inheriting class may be used wherever objects of the superclass are demanded

A

2, 3, 4

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

Which of the following is true for SVN?
1) Is the abbreviation for Subversion
2) Is a distributed version control system
3) Can check-out parts of projects (e.g., individual directories)
4) Is not used anymore

A

1, 2, 3

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

Which of the following is true for Git?
1) Is a distributed version control system
2) Has good support for non-linear workflows (branching and merging)
3) Can be used solely via command line
4) Has a local and a remote repository

A

All

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

Mention the primitive Java types

A

Boolean:
boolean

Character:
char

Integer:
byte, short, int, long

Floating-point
float (remember f after number: 1.3f). double

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

Primitive types have no ….?

A

Methods and attributes

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

Non-primitive java types

A

Enums:
enum – user defined enumeration

Arrays:
int[] – multiple values (fixed number) of same type

Collections:
List<?> – multiple values (dynamic number) of same type
Map<?,?> – dictionary

Classes:
Person – User-defined types with attributes and methods

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

How do you initialise an array of size 5 with integers?

A

Empty:
int[] myIntArray = new int[5];

With values:
int[] myIntArray = {1, 2, 3, 4, 5};

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

How do you initialise an arrayList of size 5 with integers?

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

Write an if-else statement checking if i = 3 is above 5

A

int i = 3;
if (i > 5) {
System.out.println(“yes above 5”);
}
else{
System.out.println(“no, below 5”);
}

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

Write a switch statement that checks which day in the week it is (number), and if it’s a weekend (6 or 7) prints yes the weekend, or else print no not the weekend yet.

A

int day = 6;

switch (day) {
case 6:
System.out.println(“yes it’s the
weekend, it’s saturday”);
break;

case 7:
    System.out.println("yes it's the  weekend, it's sunday");  break;

default:
System.out.println(“No, not the weekend yet”);
}

21
Q

What is the difference between a while loop and do… while loop?

A

The do… while loop is executed at least one (the first iteration), even if the condition is false from beginning.

22
Q

Write a for each statement, looping over a list of strings. List is called colours.

A

String colours = {“red”, “green”, “blue”};

for (colour : colours) {
System.out.println(colour);
}

23
Q

What should I use to check equality between strings?

A

String.equals()

String message = “HI”;

message.equals(“Hi”);

24
Q

What is a data element in a class called?

A

attribute

25
Q

What is a function in a class called?

A

A method

26
Q

What is the umbrella term for attributes, methods and constructors?

A

Members (of a class)

27
Q

An attribute constitutes of:

A

modifier type name

in that order

28
Q

When do we use void for a method?

A

When nothing is returned

29
Q

example of method, that add two integers.

A

public int add(int x, int y){
return x + y; }

30
Q

Inheritance Terminology:
Take the case of Car and ElectricCar
Both Java and UML

A

ElectricCar extends Car
ElectricCar inherits from Car

Car is the super class of ElectricCar
ElectricCar is the sub class of Car

UML:
ElectricCar is a specialisation of Car
Car is a generalisation of ElectricCar

31
Q

What does a package in Java do?

A

Group together classes.
Allows for using the same name of class, as long they are in different packages.

32
Q

Explain encapsulation and how it is implemented

A

Regulate access to members from the outside of the class.
Making your attributes private. Can only access through getters and setters.

33
Q

Visibility modifier public.
Who has access and what sign to indicate in UML.

A

Everyone has access to the class, method, field, constructor.
So within class, outside the class, within package and outside package.

+ in UML

34
Q

Visibility modifier private
Who has access and what sign to indicate in UML.

A

Only within the class. Cannot be accessed from outside the class.

  • (minus) in UML
35
Q

Visibility modifier protected.
Who has access and what sign to indicate in UML.

A

Accessible within the same package and by subclasses, regardless of the package.
# in UML

36
Q

Visibility modifier default.
Who has access?

A

Only within the package. This is if you don’t specify any access level.

37
Q

Polymorphism

A

Specialisation can be used where generalisation is requested.
When someone is expecting Car, they should be fine with getting GasolineCar or ElectricCar. Or whatever specialisation.

38
Q

Method overriding

A

Can provide an alternative implementation of a method than the one it inherited from the superclass.
@override
Can completely change the functionality of the method.
Is part of polymorphism.
Can only relax visibility modifiers.

39
Q

Method overloading

A

Overloading provides multiple implementations, at the same time, of a similarly named method with different parameter lists.
Allows for multiple methods with same name, we just need the parameter list to be unique in terms of types.

40
Q

Abstraction

A

Only necessary information. Focus on essentials - leave out details.
An object should be able to act as a substitute for its real-world counterpart.

41
Q

Static type

A

Known during development. Determines a set of known methods and attributes.

Pet myPet = new Dog();

Pet is the static (known) type.

This is also valid:
myPet = new Cat();

The type is the same.
This also means we can’t myPet.bark() bc it’s unknown.

42
Q

Abstract classes

A

Cannot be instantiated. And abstract methods do not have an implementation yet.

43
Q

Dynamic type

A

Known during execution. Determines set of existing attributes and methods. The dynamic type of a variable is the type of the value in the variable. The term has meaning only when
the program is running, and the dynamic type may be changed often as new values are assigned to the
variable.

44
Q

Interfaces

A

A 100% abstract class and has only abstract methods. The methods are not implemented.
A class can implement several interfaces.

45
Q

What is the role of a constructor?

A) Initialize an instance of a class, an object, upon its creation

B) Create names for methods

C) To create some type of change in the state of an object

D) Help in the construction of an algorithm through auto completion

A

A

The primary role of a constructor in object-oriented programming is to initialize the state of an object when it is created. It is responsible for setting initial values for the attributes or properties of an object, preparing the object for use. Constructors are typically called automatically when an object is instantiated from a class.

46
Q

Class B inherits from class A, what cannot be said?
A) B is a subclass of A
B) A is a super class of B
C) B is a generalization of A
D) B is a specialization of A

A

C

47
Q
A
48
Q
A