Data Structure

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Given a 5 element queue Q (from front to back: 1, 3, 5, 7, 9), and an empty stack S, remove the elements one-by-one from Q and insert them into S, then remove them one-by-one from S and re-insert them into Q. Select, what are the contents of the queue now look like (from front to back)?

9, 7, 5, 3, 1

Which of the following best describes an interface in Java?

An interface is similar to an abstract class that has only abstract methods.

What is the need for a circular array compared to a non-circular array-based implementation of a queue?

Effective usage of memory

Which of the following is true about linked list implementation of queue? In push operation, if new nodes are inserted at the beginning of linked list, then in pop operation, nodes must be removed from end. In push operation, if new nodes are inserted at the end, then in pop operation, nodes must be removed from the beginning.

In push operation, if new nodes are inserted at the beginning of linked list, then in pop operation, nodes must be removed from end. In push operation, if new nodes are inserted at the end, then in pop operation, nodes must be removed from the beginning.

What is the role of the protected keyword in Java?

It is an access specifier that provides a level of access somewhere between public and private.

Assuming that A is an interface, which of the following is true?

It is possible to declare a variable of type A.

What is the output of the following code? for ( int count = 0; count < 9; ) System.out.print( count + " " ); count++ ; System.out.println( );

Nothing --- the program will not compile.

Given a collection of algorithms that runs on O(1), O(n log n), O(n), O(n2 ), O(log n), O(n!), order the algorithms from fastest to slowest.

O(1), O(log n), O(n), O(n log n), O(n2 ), O(n!)

For the following code, count the number of operations where some_statement is executed based on the loops for (int j = 1 ; j < n ; j *= 2) { for (int I = 1; i < n ; i++) { some_statement ; } }

O(n logn)

Finding the max element in an unordered stack would require

O(n) operations

What is the BigO notation (in terms of n) for the following code: void f2(int n) { for(int i=0; i < n; i++) { for(int j=0; j < 10; j++) { for(int k=0; k < n; k++) { for(int m=0; m < 10; m++) { System.out.println("!"); } } } } }

O(n2)

Which of the following statements about stacks is incorrect?

Stacks are first-in, first-out (FIFO) data structures.

Which of the following is described as "dynamic binding"?

The JVM dynamically determines which method to call based on the object's type at run-time.

How do we indicate in a UML diagram that a class is abstract?

The class name is in italics.

Which of the following is an important difference between arrays and ArrayLists in Java?

The size of an array cannot change. ArrayLists can grow or shrink as needed.

Which of the following accurately describe the relationship between a subclass and a superclass. Check all that are true.

The subclass is a specialized version of the superclass. There is an "is a" relationship between the subclass and the superclass. An instance of the subclass "is a" instance of the superclass as well.

Which of the following best describes the difference between overloading and overriding a method?

When one method overrides another, both methods will have the same signature.

What will be printed by the following code snippet, assuming that letters is an initially empty? ---Code--- letters.add("A"); letters.add("B"); letters.add("C"); letters.add(1, "D"); System.out.println(letters.toString());

[A, D, B, C]

What will be the output of the following program? ---Code--- class W { static int c = 0; public static void main(String[] args) { W w1 = c(); W w2 = c(w1); W w3 = c(w2); W w4 = c(w3); } private W() { System.out.println("c = " + c); } static W c() { return c++ <= 0 ? new W() : null; } static W c(W w) { return w.c++ == 1 ? new W() : null; } }

c = 1 c = 2

Which of the following is valid, given that the following variables are declared: int cost, double taxRate, float interestRate, long count

count = cost;

The diagram below suggests how we could implement a double-ended linked list, in which we maintain a reference to both the first and last nodes in the linked list. Which one of the following operations would be inefficient to carry out when there are a large number of elements in the linked list?

deletion from the end to which rear refers

Say that class Person has a child class Student and anothr child class Staff. Class Staff has a child class HourlyStaff. Examine the following Person p; Student su_student = new Stduent(); Staff su_staff = new Staff(); HourlyStaff h_staff = new HourlyStaff(); Which one of the following will cause a compiler error? p = su_student; h_staff = null; p = su_staff; h_staff = su_student;

h_staff = su_student;

Assume that the following method header is for a method in class A. public void displayValue(int value) Assume that the following code segments appear in another method, also in class A. Which contains a legal call to the displayValue method?

int x = 7; displayValue(x);

Consider the following partially completed class public class Hat { private int size; //other code goes here } Which of the constructors below will function correctly?

public Hat(int size) { this.size = size; }

You have a singly linked list constructed out of nodes defined as follows: public class Node { public int datum; public Node next; } In all of functions shown below, the parameter first refers to the first node in the linked list, if there is one, and has the value null otherwise. Which of the following functions correctly inserts a value x at the front of the linked list and returns a reference to the new front of the linked list?

public Node insertFront(Node first, int x) { Node n = new Node(); n.datum = x; n.next = first; return n; }

The stack data type is restrictive in a sense that you cannot

remove the bottom element

What is the reason for using a "circular queue" instead of a regular one?

reuse empty spaces

Examine the following code. What will be the output of the following code?

true false

How would you access elements of an aggregated object (think of a collection) sequentially without exposing the underlying structure of the object?

using an iterator

Classes with static methods are sometimes referred to as...

utility classes

How many arguments are passed to the printf method? System.out.printf("Hello %s, the time is %02d:%02d.\n", first + " " + last, hour - 2, minute);

4 method: first, last, hour, minute

If final int SIZE = 15 and int[] x = new int[SIZE], what would be the range of subscript values that could be used with x[]?

0-14

If the sequence of operations - push (5), push (0), pop, push (9), push (2), pop, pop, pop, push (2), pop are performed on an initially empty stack, the sequence of popped out values is:

02952

What is the postfix version of the infix expression 12 / 3 * (50 / 3 * (3 / 4)) / 2 * 10

12 3 / 50 3 / 3 4 / * * 2 / 10 *

A stack S of integers initially contains the following data: 6 (top) 2 7 3 The following code is then executed: int x = S.pop(); int y = S.pop(); int z = S.pop(); S.push(x + y); int w = S.pop(); S.push(w + z); After this code has been executed, what are the contents of the stack?

15 3

What will be the output of the program? class Output { final static short i = 2; public static int j = 0; public static void main(String [] args){ for (int k = 0; k < 3; k++) { switch (k) { case i: System.out.print(" 0 "); case i-1: System.out.print(" 1 "); case i-2: System.out.print(" 2 "); } } } }

2 1 2 0 1 2:

What is the value of the variable x after the following Java commands are executed, assuming they compile and run without an exception? double x; x = (5 / 4) * Math.abs(-257) + 3.0;

260: 5/4 is 1 because int/int is a int and we throw away the remainder

Assume that you want to create an array containing ten Die objects. Which of the following code segment accomplishes this correctly? A: ---Code--- Die[] dice = new Die[10];for (int i = 0; i < dice.length; i++) { dice[i] = new Die(); } ---Code--- B: ---Code--- Die[] dice = new Die[10];Die die = new Die();for (int i = 0; i < dice.length; i++) { dice[i] = die; } ---Code---

A

Which of the following is true?

A class may implement any number of interfaces.

Which of the following statements is true?

A reference variable my hold object references of non-matching types, as long as the object type is a subclass of the variable type.

Which of the following is true of superclass constructors? Check all that are true.

A superclass constructor is always called before the subclass constructor. The "super" keyword may be used to explicitly call a superclass constructor from the subclass.

What is the correct postfix version of the infix expression A * (B + C * D) + E

ABCD*+*E+

What is the Postfix version of the following expression: ( ( AX + ( B * CY ) ) / ( D E ) )

AX B CY * + D E /

Which of the following statements is true?

Abstract classes may have constructors, even though they cannot be instantiated.

Which of the following statements is true?

Abstract classes may include both abstract and non-abstract methods.

Assume that class A does not define a toString method, and does not explicitly extend any other class. What will happen if we call toString on an object of class A?

All classes implicitly inherit from Object, so the toString method defined by Object will be called.

What will be the output of the following program? public class Time { int a = 50; int b = 10; public static void main(String args[]) { a += b--; System.out.println(a);

Compilation Error or Runtime Error:

Every node (except of the last node) in a singly linked list contains

the address of the next node

The postfix expression 14 2 5 + = will generate an error, because

there will be too many elements in the stack when the equal sign is encountered

Examine the following code: String str = "Hot Java"; boolean switch = str instanceof String; What value is placed in switch?

true


Kaugnay na mga set ng pag-aaral

Chapter 25: The Child with Gastrointestinal Dysfunction

View Set

The Canary Island Quiz Questions

View Set

CHAPTER 8 LESSON 4: DIETARY FAT RECOMMENDATIONS

View Set

Intermediate Accounting 2 Final Exam

View Set