T7 Introduction To Java Flashcards
Discuss the benefits of using Java
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
What is an Accessor method?
A public method that retrieves a property
What is a mutator method?
A public method that changes a property
What is method overloading?
Overloading is defining the same method in several versions, using the same name
How objects are created/ declared in Java?
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();
Describe the purpose of comments
To describe the program
To explain the meaning of code
improve the readability
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; }
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
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"); } }
One
Two
ThreeFour
FiveSix
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) );
The output is 12
The output is 3
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(); } }
- Data member A is a constant
- Data member c is private
- The method is private
- Wrong number of arguments and the method is private
- Wrong number of arguments
- Data member c is private
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
False
False (values for x and y are irrelevant)
True
True
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)
false (value for x is irrelevant)
true
false
true
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; }
if (grade == 10 || grade == 9) { a = 1; b = 2; } else if (grade == 8) { a = 3; b = 4; } else { a = 5; }
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) ;
Infinite loop: i is always greater than 0.
b. until is not a valid reserved word in Java. while should be used.
Identify all the errors in the following repetition statements.
while ( x < 1 AND x > 10) { a = b; } while ( a == b ); { a = b; x = y;
The conditional expression is never true.
d. Syntax error: no semicolon after the conditional expression