T7 Introduction To Java Flashcards

1
Q

Discuss the benefits of using Java

A
Platform independent 
Does not rely on platform code
Code does not change between platform
Java is Safe: Does not exhibit problem associated to memory management issue in C
High Performance
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is an Accessor method?

A

A public method that retrieves a property

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

What is a mutator method?

A

A public method that changes a property

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

What is method overloading?

A

Overloading is defining the same method in several versions, using the same name

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

How objects are created/ declared in Java?

A

Object declaration: it reserves a
small amount (like four bytes) of memory to store a reference to an object.
Account acct;
Object creation allocates the memory space for the object itself
acct = new Account();

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

Describe the purpose of comments

A

To describe the program
To explain the meaning of code
improve the readability

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
Identify the type of multipicity from A to B and reverse
Class Class A {
     Class B b;
}
Class Class B {
     Class A a;
}
Class Class A {
     Class [ ] B b;
}
A

Class A require one of B
Class B require one of A
This is a one to one relationship

Class A require many of Class B

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
Determine the output of the following program:
class TestOutputBox {
public static void main(String[] args) {
System.out.println("One");
System.out.print("Two");
System.out.print("\n");
System.out.print("Three");
System.out.println("Four");
System.out.print("\n");
System.out.print("Five");
System.out.println("Six");
}
}
A

One
Two
ThreeFour
FiveSix

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

Determine the output of the following code:
int x, y;
x = 1;
y = 2;
System.out.println(“The output is “ + x + y);
System.out.println(“The output is “ + (x + y) );

A

The output is 12

The output is 3

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
Consider the following class declaration.
class QuestionOne {
public final int A = 345;
public int b;
private float c;
private void methodOne( int a ) {
b = a;
}
public float methodTwo() {
return 23;
}
} 
Identify invalid statements in the following main class. For each invalid statement, state 
why it is invalid.
class Q1Main {
public static void main( String[] args ) {
QuestionOne q1;
q1 = new QuestionOne();
1 q1.A = 12;
q1.b = 12;
2 q1.c = 12;
3 q1.methodOne( 12 );
4 q1.methodOne();
5 System.out.println( q1.methodTwo( 12 ) );
6 q1.c = q1.methodTwo();
}
}
A
  1. Data member A is a constant
  2. Data member c is private
  3. The method is private
  4. Wrong number of arguments and the method is private
  5. Wrong number of arguments
  6. Data member c is private
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

X = 10, Y = 20, Z = 30
If statement - identify which will result in a true Boolean result
> Identify useless variable
x < 10 || x > 10
x > y AND y > x
(x < y + z) AND (x + 10 <= 20)
z - y == x AND Math.abs(y - z) == x

A

False
False (values for x and y are irrelevant)
True
True

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

X = 10, Y = 20, Z = 30
If statement - identify which will result in a true Boolean result
> Identify useless variable
x < 10 AND x > 10
x > y || y > x
!(x < y + z) || !(x + 10 <= 20)
!(x == y)) AND(x != y) AND (x < y || y < x)

A

false (value for x is irrelevant)
true
false
true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
This this into an If and else Statement
switch (grade) {
case 10:
case 9: a = 1;
b = 2;
break;
case 8: a = 3;
b = 4;
break;
default: a = 5;
break;
}
A
if (grade == 10 || grade == 9) {
a = 1;
b = 2;
}
else if (grade == 8) {
a = 3;
b = 4;
}
else {
a = 5;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
Identify all the errors in the following repetition statements. 
for (int i = 10; i > 0; i++ ) {
      x = y;
      a = b;
 }

sum = 0;
Scanner scanner = new Scanner(System.in);
do {
      num = scanner.nextInt();
      sum += num;
 } until (sum > 10000) ;
A

Infinite loop: i is always greater than 0.

b. until is not a valid reserved word in Java. while should be used.

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

Identify all the errors in the following repetition statements.

while ( x < 1 AND x > 10) {
       a = b;
 }
while ( a == b );
{
       a = b;
       x = y;
A

The conditional expression is never true.

d. Syntax error: no semicolon after the conditional expression

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

Identify all the errors in the following repetition statements.
for ( int i = 1.0; i <= 2.0; i += 0.1 ) {
x = y;
a = b;
}

A

i should be a double or the initial, condition and increment values should