1. Given an integer stack and one performs some push( ) operations in the order shown

1. Given an integer stack and one performs some push( ) operations in the order shown
below:
push(10);
push(20);
push(30);
push(40);
push(50);
1.1. What is the value of x and y below?
X = pop( );
Y = pop( );
1.2. After doing the two pops shown above, which value is on the top of the stack?
1.3. What is the current size of stack after step 1.2?
1.4. After step 1.2, what if one performs another push, push(100), then how many pop( ) operations are
needed to make integer 20 become the top element of the stack?
2. As we know, the back button (Go back one page) in our Browser relies on a stack to hold
pages we visited previously, how about the forward button (Go forward one page)? Assume that
we visited several pages according to the order shown below:
pageA, pageB, pageC, pageD, pageE.
Assume that we are at pageE now. Please answer the questions below.
2.1) if we click the back button 2 times, then what page will be shown in our Browser?
2.2) after step 2.1, if we click the forward button, what page will be loaded?
2.3) after step 2.2, can we go to pageE? If yes, how many times we need to click the forward button? If
no, explain.
2.4) based on your observation, what kind of data structure is appropriate for the forward button to hold
page information?
3. Assuming that we have defined an “ArrayList x” to hold integers. Obviously, x is empty
at the beginning. Answer the questions below:
3.1) How do you define that x? Please give the entire statement.
3.2) If the answer you used in 3.1 includes keyword “ArrayList“, please explain why you use
“Integer” instead of “int”. If the answer you used in 3.2 includes keyword “ArrayList“, please
explain why you use “int” instead of “Integer”?
3.3) How do you put integer 123 into x? Please give the complete statement.
3.4) How do you know the number of elements in x? Please give the complete expression.
3.5) Assuming that integer 123 is at index 0, and integer 456 is at index 3. How do you swap these two
items in x (that means, index 0 will have value 456, and index 3 will have value 123, others unchanged)?

4. Will the following class compile? If yes, please explain what the constructor does. If not,
why?
public class ABC {
T value;
public ABC( ) {
value = new T( );
}
}
5. Read the code part below and answer the questions.
ArrayList x = new ArrayList<>();
x.add(123);
x.add(456);
x.add(789);
x.add(900);
x.add(1000);
5.1) Is it ok to call statement x.get(x.size( ) – 1) ? If yes, what is the return value of it? If no, explain.
5.2) If one does the following two statements, then what is the value of variable A?
x.remove(1);
int A = x.get(0);
5.3) After step 5.2, is it ok to do statement int B = x.get(1)? If yes, what is the value of B? If no, explain.
5.4) After step 5.3, if one does x.add(2000), then what integers are stored in “ArrayList x”? Please list all
of them in order.
6. What is a Java Exception? Can you list TWO examples of Java exception?
7. What is a try-catch block? Why you need to try and catch?
8. What exception will be triggered by the code part below? What will that exception result
in?
public class Question8 {
public static void main(String[ ] args) {
int w = new int[20];
w[0] = 123;
w[5] = 123;
w[10] = 123;
w[15] = 123;
w[20] = 123;
}
}
9. How do you protect the code part in question 8, that means, how do you catch the
exception triggered in question 8. Please write down your code solution.

Complete Answer:

Get Instant Help in Homework Asap
Get Instant Help in Homework Asap
Calculate your paper price
Pages (550 words)
Approximate price: -
Open chat
1
Hello 👋
Thank you for choosing our assignment help service!
How can I help you?