Docsity

CICS 210 code segment practice exam questions updated, Exams of Advanced Education

Toggle sidebar
bg1
1 / 5
CICS 210 code segment practice exam questions updated
1.
Write the code for a generic Node class.:
public class Node<E> { public E data;
public Node<E>
next;
public
Node(E
e)
{
data
=
e;
}
public
Node(E
e,
Node<E>
n)
{
data
=
e;
next
=
n;
}
}
2.
Write
a
generic
method
that
prints
every
element
in
a
List.:
static <E>
void print-
All(List<E>
l)
{
for
(E
element
:
l)
{
System.out.println(element);
}
}
3.
Write code to create an ArrayList of Integers.: List<Integer> l = new
ArrayList<>();
4.
Write
code
to
create
a
LinkedList
of
Integers.:
LinkedList<Integer>
l
=
new
LinkedList<>();
5.
Write
a
copy
constructor
for
a
List.:
List<Integer> copy = new
ArrayList<>(original);
6. Write a method that returns a slice of a List from index i through j -
1.:
static
<E>
List<E>
slice(List<E>
l,
int
i,
int
j)
{
List<E>
result
=
new
ArrayList<>();
for
(int
k
=
i;
k
<
j;
k+
+)
{
result.add(l.get(k));
}
return
result;
}
7. Write a method that repeats an element a given number of times.:
static
<E>
List<E>
repeat(E
element,
int
count)
{
List<E>
result
=
new
ArrayList<>();
for
(int
i
=
0;
i
<
count;
i++)
{
result.add(element);
}
return
result;
}
8.
Write
a
method
that
creates
a
range
of
integers
using
start,
stop,
and
step.:
static
List<Integer>
range(int
start,
int
stop,
int
step)
{
List<Integer>
result
=
new
ArrayList<>();
for
(int
i
=
start;
i
<
stop;
i
+=
step)
{
result.add(i);
}
return
result;
}
9.
Write a method that removes duplicate elements from a List.: static <E>
List<E>
distinct(List<E>
l)
{
List<E>
result
=
new
ArrayList<>();
for
(E
element
:
l)
{
if
(!
result.contains(element))
{
result.add(el-
ement);
}
}
return
result;
}
10.
Write a method that filters a List to keep only even integers.: static
List<Integer>
filterEven(List<Integer>
l)
{
List<Integer>
result
=
new
ArrayList<>();
for
(Integer
x
:
l)
{
if
(x
%
2
==
0)
{
result.add(x);
}
}
return
result;
}
11.
Write a method that takes every nth element from a List.: static <E>
List<E>
takeNth(List<E>
l,
int
n)
{
List<E>
result
=
new
ArrayList<>();
for
(int
i
=
n
-
1;
i
<
pf3
pf4
pf5

Partial preview of the text

Download CICS 210 code segment practice exam questions updated and more Exams Advanced Education in PDF only on Docsity!

CICS 210 code segment practice exam questions updated

  1. Write the code for a generic Node class.: public class Node { public E data; public Node next; public Node(E e) { data = e; } public Node(E e, Node n) { data = e; next = n; } }
  2. Write a generic method that prints every element in a List.: static void print-All(List l) { for (E element : l) { System.out.println(element); } }
  3. Write code to create an ArrayList of Integers.: List l = new ArrayList<>();
  4. Write code to create a LinkedList of Integers.: LinkedList l = new LinkedList<>();
  5. Write a copy constructor for a List.: List copy = new ArrayList<>(original);
  6. Write a method that returns a slice of a List from index i through j - 1.: static List slice(List l, int i, int j) { List result = new ArrayList<>(); for (int k = i; k < j; k+ +) { result.add(l.get(k)); } return result; }
  7. Write a method that repeats an element a given number of times.: static List repeat(E element, int count) { List result = new ArrayList<>(); for (int i = 0; i < count; i++) { result.add(element); } return result; }
  8. Write a method that creates a range of integers using start, stop, and step.: static List range(int start, int stop, int step) { List result = new ArrayList<>(); for (int i = start; i < stop; i += step) { result.add(i); } return result; }
  9. Write a method that removes duplicate elements from a List.: static List distinct(List l) { List result = new ArrayList<>(); for (E element : l) { if (! result.contains(element)) { result.add(el-ement); } } return result; }
  10. Write a method that filters a List to keep only even integers.: static List filterEven(List l) { List result = new ArrayList<>(); for (Integer x : l) { if (x % 2 == 0) { result.add(x); } } return result; }
  11. Write a method that takes every nth element from a List.: static List takeNth(List l, int n) { List result = new ArrayList<>(); for (int i = n - 1; i <

l.size(); i += n) { result.add(l.get(i)); } return result; }

  1. Write a method that concatenates two Lists.: static List concatenate(List l1, List l2) { List result = new ArrayList<>(l1); result.addAll(l2); return result; }
  2. Write a method that flattens a List of Lists.: static List flatten(List> l) { List result = new ArrayList<>(); for (List inner : l) { result.addAll(inner); } return result; }
  3. Write the fields for an ArrayList implementation.: private E[] array; private int size;
  4. Write the fields for a LinkedList implementation.: private Node head; private Node tail; private int size;

studentID.compareTo(oth-er.studentID); } }

  1. Write the recursive factorial method.: static int factorial(int n) { if (n == 0) return 1; else return n * factorial(n - 1); }
  2. Write a recursive method to sum a linked list of Integers.: static int sum(Node n) { if (n == null) return 0; else return n.data + sum(n.next); }
  3. Write a recursive method to find the size of a linked list.: static int size(Node n) { if (n == null) return 0; else return 1 + size(n.next); }
  4. Write a recursive method to check whether a linked list contains an ele-ment.: static boolean contains(Node n, E e) { if (n == null) return false; else if (n.data.equals(e)) return true; else return contains(n.next, e); }
  5. Write a recursive method to check whether all integers in a linked list are odd.: static boolean allOdd(Node n) { if (n == null) return true; else if (n.data % 2 == 0) return false; else return allOdd(n.next); }
  6. Write a recursive method to sum an array.: static int sumRecursive(int[] a, int i) { if (i == a.length) return 0; else return a[i] + sumRecursive(a, i + 1); } static int sum(int[] a) { return sumRecursive(a, 0); }
  7. Write a method that calculates the mean of a double array.: static double mean(double[] a) { double sum = 0; for (int i = 0; i < a.length; i++) { sum += a[i]; } return sum / a.length; }
  8. Write a method that checks whether a List contains an element.: static boolean contains(List l, E e) { for (E element : l) { if (element.equals(e)) return true; } return false; }
  9. Write a method that checks whether a List contains duplicates.: static boolean containsDuplicate(List l) { for (int i = 0; i < l.size(); i++) { for (int j = i + 1; j < l.size(); j++) { if (l.get(i).equals(l.get(j))) return true; } } return false; }
  10. Write a one-loop method to sum all integers in an array.: static int sum(int[] a) { int sum = 0; for (int i = 0; i < a.length; i++) { sum += a[i]; } return sum; }
  11. Write a for loop that goes through every element of an array.: for (int i = 0; i < a.length; i++) { System.out.println(a[i]); }
  12. Write an enhanced for loop that goes through every element of a List.: for (Integer x : numbers) { System.out.println(x); }
  13. Write code that checks if an integer is even.: if (x % 2 == 0) { }
  14. Write code that checks if an integer is odd.: if (x % 2 != 0) { }
  15. Write code that checks whether an iterator has another element before calling next().: if (it.hasNext()) { System.out.println(it.next()); }
  16. Write code that compares two objects using equals().: if (a.equals(b)) { }
  1. Write code that checks whether two references point to the same object.: if (a == b) { }
  2. Write a constructor that stores a name and age.: public Student(String n, int a) { name = n; age = a; }