Mcq 15-20
Q15:
Which of the following trees are binary search trees?
. all of them
Q16:
Which of the following trees are binary search trees?
. all of them
Q17:
. all of them
Q18:
It is forbidden to place both private and abstract in front of a method declaration.
. true
. false
Q19:
Consider the binary search tree below:
What is the post-order path of this binary search tree?
Remember this in the post-order walk: First, we go through the left subtree. Then we cross the right subtree. Finally, we visit the root.
Q20:
Consider the following two classes:
After executing the code, we will have the following display:
Mcqs 7- ?
Q7:
Suppose A is an interface, B is a class that realizes interface A, and C is a class that extends class B.
Consider the following code:
Which of the test () method statements will print true to standard output?
Reminder: The java.lang.Object.getClass () method returns the runtime class of an object.
Q8:
Which ArrayList declaration is appropriate for a list that contains values of type float?
Q9:
What does the following method do in the context of a simply linked list?
. The method removes all nodes from the linked list.
. The method traverses the linked list without modifying it.
. The method reverses the linked list.
. The method removes every other node from the linked list.
Q10:
Suppose we have a single-linked sorted list whose nodes contain integer values. What is the mystery () method for in the following code snippet?
Question 10 options:
.The method removes every other node from the linked list.
.This method removes duplicates from the linked lis
.The method traverses the linked list without modifying it.
Q11:
Complete lines 27 and 28 of the following implementation (blanks 1 and 2) so that addLast (E value) adds a value to the end (back) of the doubly linked list.
Q12:
Consider the following doubly linked list. The values saved in the list are integers.
Now, suppose we run the tweak () method, shown below, on this doubly linked list.
Starting from the node designated by head, how will the values of the nodes be read after executing tweak ()?
Q13:
Consider a simply linked list. We want to write a private recursive method, E size (Node <E> current), to calculate the size of the list starting with the node designated by head via the public E size () method (also shown in the snippet below).
Which of the following choices corresponds to a correct implementation of the E size (Node <E> current) method?
Q13 options :
Q14:
The following List interface declares a subset of the methods of the java.util.List interface.
public interface List <E> {
void add ( int index , E elem ) ;
E remove (int index ) ;
boolean remove (E obj ) ;
int size ( ) ;
boolean isEmpty ( ) ;
//autres méthodes
}
For implementing this interface using an array, ArrayList.
Question 14 options:
.Insertions at intermediate positions in the list are always quick.
.Removing an item is always quick.
.Consulting the i ...
Mcq 15-20Q15Which of the following trees are binary search tr
1. Mcq 15-20
Q15:
Which of the following trees are binary search trees?
. all of them
Q16:
Which of the following trees are binary search trees?
. all of them
Q17:
. all of them
Q18:
It is forbidden to place both private and abstract in front of a
method declaration.
. true
. false
Q19:
Consider the binary search tree below:
What is the post-order path of this binary search tree?
Remember this in the post-order walk: First, we go through the
left subtree. Then we cross the right subtree. Finally, we visit
the root.
Q20:
Consider the following two classes:
After executing the code, we will have the following display:
2. Mcqs 7- ?
Q7:
Suppose A is an interface, B is a class that realizes interface A,
and C is a class that extends class B.
Consider the following code:
Which of the test () method statements will print true to
standard output?
Reminder: The java.lang.Object.getClass () method returns the
runtime class of an object.
Q8:
Which ArrayList declaration is appropriate for a list that
contains values of type float?
Q9:
What does the following method do in the context of a simply
linked list?
. The method removes all nodes from the linked list.
. The method traverses the linked list without modifying it.
. The method reverses the linked list.
. The method removes every other node from the linked list.
Q10:
Suppose we have a single-linked sorted list whose nodes contain
integer values. What is the mystery () method for in the
following code snippet?
Question 10 options:
.The method removes every other node from the linked list.
.This method removes duplicates from the linked lis
.The method traverses the linked list without modifying it.
Q11:
3. Complete lines 27 and 28 of the following implementation
(blanks 1 and 2) so that addLast (E value) adds a value to the
end (back) of the doubly linked list.
Q12:
Consider the following doubly linked list. The values saved in
the list are integers.
Now, suppose we run the tweak () method, shown below, on this
doubly linked list.
Starting from the node designated by head, how will the values
of the nodes be read after executing tweak ()?
Q13:
Consider a simply linked list. We want to write a private
recursive method, E size (Node <E> current), to calculate the
size of the list starting with the node designated by head via the
public E size () method (also shown in the snippet below).
Which of the following choices corresponds to a correct
implementation of the E size (Node <E> current) method?
Q13 options :
Q14:
The following List interface declares a subset of the methods of
the java.util.List interface.
public interface List <E> {
void add ( int index , E elem ) ;
E remove (int index ) ;
boolean remove (E obj ) ;
int size ( ) ;
boolean isEmpty ( ) ;
//autres méthodes
}
4. For implementing this interface using an array, ArrayList.
Question 14 options:
.Insertions at intermediate positions in the list are always quick.
.Removing an item is always quick.
.Consulting the intermediate positions of the list is always fast.
.none of the above
Q15:
Consider the following simply chained list (the values stored in
the list are of type String).
What values will be stored in the list read from left to right
after calling the mystery (head, "C") method, shown below,
from the list above?
Mcqs :
Q1:
Suppose we have a queue q (front to back: 2, 4, 6, 8, 10) and an
initially empty stack s. We remove the elements from the queue
q one at a time and add them to s, until q becomes empty. Next,
we remove the elements from s one by one and put them in
queue q, until s becomes empty. What does q (front to back)
look like now?
Q2:
Consider an implementation of a queue based on a circular
table. Suppose we set the maximum capacity of the queue to 5,
or MAX_QUEUE_SIZE = 5. In other words, the underlying
array will have 5 elements, indexed 0, 1, 2, 3, 4. We define the
queue as follows :
CircularArrayQueue <String> queue = new CircularArrayQueue
5. <String> ();
Suppose the queue front variable is initially set to 0, as shown
below.
What will be the internal state of the array in the queue after
executing the following sequence of operations?
queue.enqueue("X");queue.enqueue("Z");queue.enqueue("S");qu
eue.dequeue();queue.enqueue("V");queue.dequeue();queue.enqu
eue("C");queue.enqueue("N");queue.dequeue();
Q3:
Either the following code:
Which of the following options is correct?
Question 3 options:
This code is correct (it can be compiled and executed)
This code is not correct (it cannot be compiled and executed)
Q4:
What is the result of the following program?
Q5:
What is the result of the following program?
Q6:
What is the result of the following program?
6. Iterator2.javaIterator2.javapublicinterfaceIterator<T>{
boolean hasNext();
T next();
}
java exam Programm 2.docx
Programm 2 :
Q26:
For this question, you are going to write a program that
transforms a given two-dimensional array (Matrix) of objects
(generic type E) into a linked grid-like structure. You will then
implement some operations as well as an iterator on the
resulting linked structure. Below, we illustrate the
transformation for a two-dimensional array of integer objects
(ie Integer [] []).
Each node in the grid is an instance of the Node (nested) class,
shown below. Each Node instance points to two other Node
instances: the node immediately to its right (right) and the node
immediately below (down). Grid lines end with a right null
pointer; columns end with a null pointer down, as shown above.
The grid has a perfectly rectangular shape; there are no missing
nodes in the grid structure.
Complete the implementation of the LinkedGrid class by writing
the methods requested in the questions that follow.
The first part of the class is below. Otherwise the complete
class is in the attached file in prohramme2: LinkedGrig.java.
/*Class LinkedGrid */
import java.util.NoSuchElementException;
public class LinkedGrid<E> {
/* Nested Node class */
7. public static class Node<T> {
private T data;
private Node<T> right, down;
Node(T data, Node<T> right, Node<T> down) {
this.data = data;
this.right = right;
this.down = down;
}
public T getData() { return data;}
public Node<T> getRight(){ return right; }
public Node<T> getDown(){ return down;}
}
/*Instance variables*/
private Node<E> topLeft; //first element of the grill
private int rowCount, columnCount; // number of rows and
number of columns
/* The rest is in the attached file in program2*/
}
Question 1) Complete the int getRowCount () method which
returns the number of rows of the grid in the space below
/*Instance methods*/
/*Returns the number of rows */
public int getRowCount() {
Votre ligne du code vient ici
}
Note for files provided attached in program 2:
You are provided with three files attached in program2:
LinkedGrid.java, the Iterator.java interface, and the Q2Test.java
test code to test your programs.
We have provided you with a Template implementation that
includes: (1) the declaration of the Node class (shown above),
8. (2) the signatures of the methods you need to implement in
Q26-Q33, (3) the skeleton code (y including the Iterator
interface) for the LinkedGridIterator class in Q33, (4) the full
implementation of the constructor for a single row grid
(LinkedGrid (E [] array)), (5) the full implementation of the
toString method () for LinkedGrid, and (6) a Q1Test class that
allows you to test your implementation.
Q27:
For the LinkedGrid class from question 26, Implement the
method in the space below,
int getColumnCount(): returns the number of columns in the
grid
/* Returns the number of columns in the grid */
public int getColumnCount () {
Your line of code comes here
}
Q28:
For the LinkedGrid class from question 26, Implement the
method,
boolean isEmpty(): renvoie true si la grille est vide
/* Returns true if the grid is empty */
public boolean isEmpty() {
Your line of code comes here
}
Q29:
For the LinkedGrid class from question 26, Implement the
method,
Node<E> getTopLeft(): returns the first element of the grid
/* Returns the first element of the grid */
public Node<E> getTopLeft(){
Your line of code comes here
9. }
Q30:
For the LinkedGrid class from question 26, Implement the
method,
E getElementAt (int row, int column): This method returns the
object stored in row i and column j.
For example, calling getElementAt (1, 2) on our illustrative
integer grid will return an Integer instance with the value 5.
/* Returns the object stored at row i and column j */
public E getElementAt(int row, int column) {
if (row < 0 || row >= rowCount || column < 0 ||
column >= columnCount)
throw new IllegalArgumentException("The both parameters
have to be within range");
Node<E> current = topLeft;
Your piece of the code comes here
return current.data;
}
Q31:
For the LinkedGrid class from question 26, Implement the
method,
void addFirstRow(E[] array);
/* create and add the first row of the grid */
private void addFirstRow(E[] array) {
if (!isEmpty())
throw new IllegalStateException("Grid must be empty to add a
first row");
10. topLeft = new Node<E>(array[0], null, null);
Node<E> current = topLeft;
Your piece of the code comes here
}
When this method is called (by the LinkedGrid constructor), the
first row of the grid is created and the topLeft instance variable
in LinkedGrid is made to point to the first node of that row. The
expected behavior of the method is illustrated in the figure
below. Additionally, the method should set rowCount instance
variables and columnCount in LinkedGrid (index: rowCount
should be set to 1 and columnCount should be set to
array.length).
Q32:
For the LinkedGrid class of question 26, complete the method
void addRow(E[] array) ; Add a line at the bottom of the grid
When called, this method adds a new row at the bottom of an
existing grid. This behavior is illustrated in the figure below.
Note that the addRow (E [] array) method can be called
iteratively, with each call adding a row to the bottom of the
grid.
To create the entire grid in our illustration, addRow must be
called twice, once with [3; 4; 5] as parameter then with [6; 7; 8]
as a parameter. Note that the first row (ie [0; 1; 2]) must have
been added beforehand by calling addFirstRow (array E [])
expanded in Q31. Calling addRow (array E []) should increase
the columnCount instance variable by one.
/* Add a line at the bottom of the grid */
public void addRow(E[] array) {
if (array == null)
throw new NullPointerException("array
cannot be null");
if (rowCount == 0)
throw new IllegalStateException("Need to
11. add first row first");
if (array.length != this.columnCount)
throw new IllegalArgumentException("array must contain " +
this.columnCount + " elements");
Node<E> prev = topLeft;
while(prev.down != null)
prev = prev.down;
Node<E> current = new Node<E>(array[0], null,
null);
prev.down = current;
Your piece of the code comes here
}
Q33/
Class LinkedGridIterator:
Complete the LinkedGridIterator () parameterless constructor of
the LinkedGridIterator class for initialization (of
currentIterator, headRow).
//constructor
public LinkedGridIterator() {
Your line of code comes here
}
LinkedGrid.javaLinkedGrid.javaimport java.util.NoSuchElemen
tException;
publicclassLinkedGrid<E>{
publicstaticclassNode<T>{
private T data;
privateNode<T> right, down;
Node(T data,Node<T> right,Node<T> down){
this.data = data;
this.right = right;
12. this.down = down;
}
public T getData(){return data;}
publicNode<T> getRight(){return right;}
publicNode<T> getDown(){return down;}
}
privateNode<E> topLeft;
privateint rowCount, columnCount;
publicLinkedGrid(E[] array){
if(array ==null)
thrownewNullPointerException("array cannot be null");
if(array.length ==0)
thrownewIllegalArgumentException("array must contain elemen
ts");
addFirstRow(array);
}
privatevoid addFirstRow(E[] array){
if(!isEmpty())
thrownewIllegalStateException("Grid must be empty to add a fir
st row");
// Add your code here
thrownewUnsupportedOperationException("not implemented yet
!");
}
publicvoid addRow(E[] array){
if(array ==null)
13. thrownewNullPointerException("array cannot be null");
if(rowCount ==0)
thrownewIllegalStateException("Need to add first row first");
if(array.length !=this.columnCount)
thrownewIllegalArgumentException("array must contain contain
"+this.columnCount +" elements");
// Add your code here
thrownewUnsupportedOperationException("not implemented yet
!");
}
publicLinkedGrid(E[][] array){
if(array ==null)
thrownewNullPointerException("array cannot be null");
thrownewUnsupportedOperationException("not implemented yet
!");
}
publicint getRowCount(){
// Add your code here
}
publicint getColumnCount(){
// Add your code here
}
publicboolean isEmpty(){
// Add your code here
}
14. publicNode<E> getTopLeft(){
// Add your code here
}
public E getElementAt(int row,int column){
if(row <0|| row >= rowCount || column <0|| column >= column
Count)
thrownewIllegalArgumentException("The row and column para
meters both have to be within range");
// Add your code here
thrownewUnsupportedOperationException("not implemented yet
!");
}
publicString toString(){
StringBuffer buffer =newStringBuffer();
for(int i =0; i < rowCount; i++){
for(int j =0; j < columnCount; j++){
buffer.append(getElementAt(i, j));
if(j < columnCount -1)
buffer.append(", ");
}
if(i < rowCount -1)
buffer.append(System.lineSeparator());
}
return buffer.toString();
}
privateclassLinkedGridIteratorimplementsIterator<E>{
16. integerGrid.addRow(integerThir dRow);
System.out.println("==== Integer grid with "+ integerGrid.getR
owCount()+
" rows x "+ integerGrid.getColumnCount()+" columns ====");
System.out.println("[Test LinkedGrid(E[] array) / addFirstRow(.
..) and addRow(...)]");
System.out.println("getTopLeft().getData(): "+ integerGrid.getT
opLeft().getData());
System.out.println("getTopLeft().getRight().getRight().getDown
().getData(): "+ integerGrid.getTopLeft().getRight().getRight().
getDown().getData());
System.out.println("getTopLeft().getDown().getDown().getData
(): "+ integerGrid.getTopLeft().getDown().getDown().getData()
);
System.out.println("getTopLeft().getDown().getRight().getDow
n().getRight().getData(): "+ integerGrid.getTopLeft().getDown(
).getRight().getDown().getRight().getData());
System.out.println();
System.out.println("[Test getElementAt(...)] Print integer grid v
ia toString() which uses getElementAt(...)");
System.out.println(integerGrid);
System.out.println();
System.out.println("[Test LinkedGridIterator] Grid via an iterat
or");
Iterator<Integer> iIterator = integerGrid.iterator();
StringBuffer iBuffer =newStringBuffer();
System.out.println();
}
publicstaticvoid main(String[] args){
test();
17. }
}
/**************************SORTIE QUE VOUS DEVEZ A
VOIR :**********************
==== Integer grid with 3 rows x 3 columns ====
[Test LinkedGrid(E[] array) / addFirstRow(...) and addRow(...)]
getTopLeft().getData(): 0
getTopLeft().getRight().getRight().getDown().getData(): 5
getTopLeft().getDown().getDown().getData(): 6
getTopLeft().getDown().getRight().getDown().getRight().getDat
a(): 8
[Test getElementAt(...)] Print integer grid via toString() which
uses getElementAt(...)
0, 1, 2
3, 4, 5
6, 7, 8
[Test LinkedGridIterator] Grid via an iterator
**************************************************** *
***************************/
Java exam.docx
Java exam:
Part 2 : Programming questions :
Two programs to be developed: Note that the two programs are
independent; program 1 (Q21-25) and program 2 (Q26-33)
18. Files for testing (Q1Test.java and Q2Test.java) are provided to
you and are attached in program 1 and program 2 respectively,
along with the interfaces and sketches of the corresponding
classes.
Program 1 :
Q 21:
For this question we develop a queue modeling using a simply
linked list of objects (of generic type D). So there is an
interface, named Queue, which is provided to you, as well as a
simple, named, LinkedQueue implementation that you are going
to make.
The LinkedQueue class realizes the Queue interface, it must
provide an implementation for all the methods of the interface:
LinkedQueue () : constructeur sans paramètre
boolean isEmpty() : renvoie true si la file (LinkedQueue) est
vide
void enqueue(D newElement): empiler la file (Insérer un
élément (newElement ))
D dequeue() : dépiler la file (retirer un élément et le renvoyer)
/* Classe LinkedQueue */
public class LinkedQueue<D> implements Queue<D> {
//nested class
private static class Elem<T> {
private T value;
private Elem<T> next;
private Elem(T value, Elem<T> next) {
this.value = value;
this.next = next;
}
}
//Instance variables
private Elem<D> front;
private Elem<D> rear;
19. //...la suite est dans le programme LinkedQueue.java fourni ci-
joint dans programme 1
}
Question 1: For this LinkedQueue class, implement the
parameterless constructor for initialization, in the space below
/* Constructor*/
public LinkedQueue () ){
Note for the files provided attached in program 1:
THREE FILES ARE ATTACHED IN PROGRAM 1: Queue.java,
LinkedQueue.java and Q1Test.java for testing.
We have provided you with an implementation that includes: (1)
the declaration of the Elem class (shown above), (2) the
signatures of the methods you need to implement in Q21-Q25,
(3) the skeleton code (including the Queue interface) for the
LinkedQueue class, (4) the full implementation of the toString
() method for LinkedQueue, (5) the skeleton code of the
UniquifiableLinkedQueue class for Q25, and a Q1Test class that
allows you to test your implementation .
Q22 :
For the LinkedQueue class from question 21, implement the
boolean isEmpty () method which returns true if the queue is
empty, in the space below.
/ * returns true if the queue is empty * /
public boolean isEmpty(){
Q23 :
For the LinkedQueue class from question 21, implement the
void enqueue (D newElement) method to stack the queue.
/* stack the queue */
public void enqueue(D newElement){
if(newElement == null) {
throw new NullPointerException("no null object in my
20. queue !");
}
Elem<D> newElem;
newElem = new Elem<D>(newElement,null);
Your piece of the code comes here
}
Q24:
For the LinkedQueue class from question 21, implement the D
dequeue () method to unstack the queue.
/* unstack the queue (remove an item and send it back)*/
public D dequeue() {
if(isEmpty()) {
throw new IllegalStateException("method called on an
empty queue");
}
D returnedValue;
returnedValue = front.value;
Your piece of the code comes here
}
Q25:
For this question, you are going to develop a specialization
(subclass) of LinkedQueue. The subclass, named
UniquiableLinkedQueue, provides a single additional method,
named uniquify ().
The uniquify () method does the following:
It returns a queue that is the same UniquifiableLinkedQueue
instance that the method is called for, except that the returned
queue does not contain any immediately consecutive duplicate
items.
21. To illustrate what "immediately consecutive duplicate elements"
are, consider the following example queue:
More precisely, two queue items are immediately consecutive
duplicate items, when they are adjacent and have identical
content. Having identical content for the (non-zero) queue
elements elem1 and elem2 means that elem1.equals (elem2) is
true. You can assume that all items in the queue are not null.
You complete the uniquify () method in the
UniquifiableLinkedQueue class below, so that a single instance
of immediately consecutive duplicate items would be kept. For
example, if uniquify () is called on the example file above (an
instance of UniquifiableLinkedQueue <String>), the result
should be as shown in the figure below.
Note that uniquify () is not intended to handle non-consecutive
duplicate items. In other words, the queue returned by uniquify
() may have duplicate items.
You can make calls to the preceding enqueue and dequeue
methods.
To test your implementation of uniquify (), you can use the
provided Q1Test.java code. The output from executing Q1Test
should be the same as that in the test code.
/*Class UniquifiableLinkedQueue*/
public
class UniquifiableLinkedQueue <E> extends LinkedQueue<E> {
/*uniquify method*/
public Queue<E> uniquify ( ) {
Queue<E> result = new LinkedQueue<E>();
Queue<E> temp = new LinkedQueue<E>();
Your piece of the code comes here
23. }
Elem<D> newElem;
newElem =newElem<D>(newElement,null);
// Add your code here
}
public D dequeue(){
if(isEmpty()){
thrownewIllegalStateException("Dequeue method called on an e
mpty queue");
}
D returnedValue;
returnedValue = front.value;
// Add your code here
}
publicString toString(){
String result ="Front -> [";
if(!isEmpty()){
Elem<D> p = front;
result += p.value;
while(p.next !=null){
p = p.next;
result +=", "+ p.value;
}
}
result +="] <- Rear";
return result;
}