Java Coding Questions and Answers, Exams of Data Structures and Algorithms

Solutions to various java coding questions, including explanations of the logic behind each answer. The questions cover topics such as arrays, loops, exceptions, interfaces, and inheritance.

Typology: Exams

2012/2013

Uploaded on 04/02/2013

shashi_16star
shashi_16star 🇮🇳

4.6

(20)

99 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Birthday: Month: ________ Day: ________
Question #1
a) What is the output of the following code? Write your answer in the box.
int[] myArray = {1, 2, 3, 4, 5};
System.out.println(myArray[4]);
myArray = new int[4];
System.out.println(myArray[3]);
b) Fill in the blanks below to indicate what is printed by running the main method of Mystery.java shown
below. There are no compile-time or run-time errors in this program.
public class Mystery {
public static void mystery1(boolean [] bArray){
boolean b;
for (int i = 0; i< bArray.length; i++){
b = bArray[i];
b = !b;
}
Mystery.mystery2(bArray); // 1
bArray[2] = false;
Mystery.mystery2(bArray); // 2
bArray = new boolean[4];
Mystery.mystery2(bArray); // 3
}
public static void mystery2(boolean [] bArray){
int i = bArray.length - 1;
while (i > 0){
System.out.print(bArray[i] + " ");
i--;
}
System.out.println();
}
public static void main(String [] args){
boolean [] bArray = {true, true, true, true, false};
Mystery.mystery1(bArray);
Mystery.mystery2(bArray); // 4
}
}
Write what is printed
// 1
// 2
// 3
// 4
6
5
0
When you create
the new array the
values are set to 0
B is a local variable so
setting it doesn’t affect
the array
Set to a new array with
default values false and
has length of 4 not 5
Printing
Backwards.
Doesn’t print the last
element. This wasn’t
intended to be tricky but
rather to try to test your
understanding of loops.
Shouldn’t be changed by
modifying the local
variable bArray in the
method mystery1. In the
main we still point to the
old array
pf3
pf4
pf5

Partial preview of the text

Download Java Coding Questions and Answers and more Exams Data Structures and Algorithms in PDF only on Docsity!

Question

a) What is the output of the following code? Write your answer in the box. int [] myArray = {1, 2, 3, 4, 5}; System. out .println(myArray[4]); myArray = new int [4]; System. out .println(myArray[3]);

b) Fill in the blanks below to indicate what is printed by running the main method of Mystery.java shown

below. There are no compile-time or run-time errors in this program.

public class Mystery { public static void mystery1( boolean [] bArray){ boolean b; for ( int i = 0; i< bArray.length; i++){ b = bArray[i]; b = !b; } Mystery. mystery2 (bArray); // 1 bArray[2] = false ; Mystery. mystery2 (bArray); // 2 bArray = new boolean [4]; Mystery. mystery2 (bArray); // 3 } public static void mystery2( boolean [] bArray){ int i = bArray.length - 1; while (i > 0){ System. out .print(bArray[i] + " "); i--; } System. out .println(); } public static void main(String [] args){ boolean [] bArray = { true , true , true , true , false }; Mystery. mystery1 (bArray); Mystery. mystery2 (bArray); // 4 } }

Write what is printed

5 0

When you create the new array the values are set to 0

B is a local variable so setting it doesn’t affect the array

Set to a new array with default values false and has length of 4 – not 5

Printing Backwards.

Doesn’t print the last element. This wasn’t intended to be tricky but rather to try to test your understanding of loops.

false true true true false true false true 0 0 false false false false true false true 0

Shouldn’t be changed by modifying the local variable bArray in the method mystery1. In the main we still point to the old array

Question

public class ExceptionalStuff { public static void crazy( int i) __________________________{ if (i == 0){ System. out .println("1"); throw new NullPointerException(); System. out .println("2"); } try { System. out .println("3"); throw new ExceptionA(); System. out .println("4"); } catch (Exception e) { System. out .println("5"); throw new ExceptionB(); System. out .println("6"); } finally { System. out .println("7"); } } a) ExceptionA, and ExceptionB all extend Exception. For the code above to compile, what must be added to the blank above? (Circle 0 or more of the words below)

throws ExceptionA, ExceptionB, NullPointerException

b) What is printed by ExceptionalStuff.crazy(0);? (You do not need to print anything for exceptions.)

c) What is printed by ExceptionalStuff.crazy(1);? (You do not need to print anything for exceptions.)

You don’t need to declare null pointer exceptions as thrown because they are unchecked.

Once the nullpointer exception is thrown – no other code executes. Because it is not inside of a try. So the 2 is never printed.

1

3 5 7

4 and 6 are never printed because they come after an exception that is thrown. The finally is always executed last.

This is thrown by the method and must be caught

All ExceptionAs that are thrown are caught so it is not necessary to add to the blank

Question

Fill in the blanks below with legal Java to produce the output indicated in each comment. If it is impossible

write “IMPOSSIBLE” in the blank. You may not create any additional objects!

public class Parent { public void feed(Parent p){ System. out .println("Parent feed Parent"); } public void feed(Child c){ System. out .println("Parent feed Child"); } } public class Child extends Parent { public void feed(Parent p){ System. out .println("Child feed Parent"); } public void feed(Child c){ System. out .println("Child feed Child"); } public static void main(String[] args) { Parent p = new Child(); // Child feed Child

// Child feed Parent

// Parent feed Child

// Parent feed Parent

p = new Parent(); // Child feed Child

// Child feed Parent

// Parent feed Child

// Parent feed Parent

} }

p.feed((Child) p)

p.feed(p)^0 (^0) Impossible

(^0) Impossible

0

Impossible (^0) Impossible

(^0) Impossible

(^0) p.feed( p)

0

Question #5 (continued on next page)

Below is a modification of code from the Account class. Read the syntactically valid code provided and debug the method removePoorParents(). This method should remove any parent from the chain of parents that has a balance less than 1,000. An Account that has their parent Account removed should still be able to access the parent of their former parent Account (Assuming that parent Account has a balance of 1000 or greater.)

a) Fill in the main method below with code to demonstrate the logical error in removePoorParents(). Also fill in the blanks to explain the error. public class Account { private Account myParent; private int myBalance; public Account( int balance, Account parent) { this .myBalance = balance; this .myParent = parent; } public void removePoorParents() { if ( this .myParent != null ) { if ( this .myParent.myBalance < 1000) { this .myParent = this .myParent.myParent; if ( this .myParent == null ){ return ; } } this .myParent.removePoorParents(); } } public static void main(String[] args) {

/* At this point _________________ is ______________________________

  • but it should be ________________________________________________ */

It keeps your parent even if your parent is poor.

Account a1 = new Account(10, null); Account a2 = new Account(10, a1); Account a3 = new Account (10, a2); a3.removePoorParents();