1/54
A set of flashcards designed to help review Java programming concepts, data structures, and relevant code outputs.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
What will this small program output?
class Main {
public static void foo() {
System.out.println(x);
}
public static int x = 42;
public static void main(String[] args) {
x = 6;
foo();
}
}
6
What is the output of this Java program?
class Driver {
public static void main(String[] args) {
int a = foo(3);
int b = bar(4);
}
static int foo(int a) {
a = bar(a + 1);
System.out.print(a);
return a;
}
static int bar(int a) {
System.out.print(a);
return a - 1;
}
}
434
What is the final value of x after conditional checks in the Driver class?
class Driver {
public static void main(String[] args) {
int a = 2;
int b = 1;
int c = 2;
int x = 1;
if (a > 1) {
x = x + 200;
}
if (b > 4) {
x = x + 10;
}
if (c > 7) {
x = x + 2;
}
System.out.print(x);
}
}
201
What is the output of this Java program?
class Driver {
public static void main(String[] args) {
int a = 2;
int b = 1;
int c = 2;
int x = 1;
if (a > 1) {
x = x + 200;
}
if (b > 4) {
x = x + 10;
}
if (c > 7) {
x = x + 2;
}
System.out.print(x);
}
}
70
Evaluate the following code to determine the output. Hint - ASCII value of A is 65.
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<Character> list = new ArrayList<>();
for (char c = 'B'; c <= 'D'; c++) list.add(c);
for (int i = 0; i < list.size(); i++) {
list.set(i, (char)(list.get(i) + 2));
}
list.add((char)(65 + 1));
list.remove(1);
list.remove(Integer.valueOf('C'));
list.add(0, (char)(list.get(1) - 1));
System.out.println((int)list.get(1));\
}
}
68
What is the output of the following code?
public static void main(String[] args) {
int[] arr = {1, 2, 5, 10, 12, 14, 16, 18, 20, 22, 28, 32};
for(int i = 0; i < arr.length; i++) {
int temp = arr[i];
arr[i] = arr[arr.length-i-1];
arr[arr.length-i-1] = temp;
}
for(int i = 0; i < arr.length; i++) {
if(arr[i] % 2 == 0) {
arr[i] = arr[i] / 2;
}
}
System.out.println(arr[4] + arr[1]);
}
7
What is the output of this Java program?
class Driver {
public static void main(String[] args) {
int a = 16;
int b = a + 14;
while (a < b) {
a = a + 2;
b = b - 1;
}
System.out.print(a + b);
}
}
51
What is the output of this Java program?
class Driver {
public static void main(String[] args) {
int a = 56;
for (int i = 6; i > 1; i--) {
a = a - i;
}
System.out.print(a);
}
}
36
Which of the following scenarios would cause a NullPointerException when working with linked lists? (Select all that apply)
1, 2
What is the output of the following recursive code?
public class Main {
public static void main(String[] args) {
System.out.print(foo("hello"));
}
public static String foo(String var) {
if (var.length() <= 1)
return var;
System.out.print(var.charAt(1));
return foo(var.substring(1));
}
}
“elloo”
Which of the following would correctly declare and instantiate a 2D array with a total of 24 ints? Choose all that apply.
int[2] values = new int[12];
int[,] values = int[2,12];
int[][] values = new int[3][8];
int[][] values = new int[2][12];
int[12] values = new int[2];
int[] values = int[24];
int[4] values = new int[8];
int[24] values = new int[24];
int[][] values = new int[12][2];
int[][] values = new int[8][3];
int[][] values = new int[24];
none of these
int[][] values = new int[2][12]; and int[][] values = new int[12][2]; int[3][8]; & int[][] values = new int[8][3];
What would be the output of the following code?
public class Foo {
public int var;
public String str;
public Foo(int var, String str) {
this.var = var;
this.str = str;
}
}
class Main {
public static void main(String[] args) {
Foo foo = new Foo(10, “bar”);
foo.var = 5;
System.out.print(foo.var + foo.str);
}
}
5bar
What would be the output of the following code?
MyStack<Integer> s = new MyStack<Integer>();
s.push(8); s.push(16);
s.pop();
s.push(32); s.push(18);
s.peek();
System.out.print(s.size());
3
Given the following declaration:
int[] values = {1,3,5,0,2,8,7,2,5,2};
Evaluate the following expression:
values[values[5]]
5
What is the output of this Java program?
import java.util.Stack;
public class Main {
public static void main(String[] args) {
Stack<Integer> s = new Stack<>();
for (int i = 1; i <= 4; i++) {
s.push(i+1);
}
while (!s.isEmpty()) {
System.out.print(s.pop()+2 + " ");
}
}
}
7 6 5 4
What is the Big O run time complexity for retrieving the value at a given index in a linked list?
O(n) linear
O(1) constant
O(log n) logarithmic
O(2^n) exponential
None of these
O(n) linear
What is the Big O run time complexity for retrieving the value at a given index in a stack?
O(n) linear
O(1) constant
O(log n) logarithmic
O(2^n) exponential
None of these
O(n) linear
What is the Big O run time complexity for removing the first value in a queue?
O(n) linear
O(1) constant
O(log n) logarithmic
O(2^n) exponential
None of these
O(1) constant
What is the Big O run time complexity for removing the top value in a stack?
O(n) linear
O(1) constant
O(log n) logarithmic
O(2^n) exponential
None of these
O(1) constant
Which of the following would have a exponential Big O run-time complexity?
Compute the area of a circle given the radius
Determine if two logical statements are equivalent by brute force
Find the phone number in a phone book, given a person’s name
Delete a value from an ArrayList
none of these
Determine if two logical expressions are equivalent by brute force
Which of the following would have a logarithmic Big O run-time complexity?
Determine if two logical statements are equivalent by brute force
Compute the area of a circle given the radius
Delete a value from an ArrayList
Find the phone number in a phone book, given a person’s name
none of these
Find the phone number in a phone book, given peron’s name
Which of the following run-time complexity orders is best?
O(log n) Logarithmic time
none of these
O(n) Linear time
O(2^n) Exponential
O(log n)
Which of the following run-time complexity orders is worst?
O(n) Linear time
O(log n) Logarithmic time
none of these
O(1) Constant time
49 O(n)
A name defined in an outer scope is not available in all blocks nested inside that scope.
True
False
false
Which keyword is used in Java to create an object?
none of these
create
build
make
new
new
Which object oriented element is best defined as "objects best viewed as packages of responsibility"?
Data Hiding
Message Passing
Encapsulation
Polymorphism
Inheritance
none of these
Composition
Encapsulation
We can more easily debug a program when the responsibilities are well encapsulated.
Group of answer choices
True
False
true
Given the following partial code, fill in the blank to complete the code necessary to insert node x in between the last two nodes. (don't forget the semicolon)
...
class Node {
public Object data = null;
public Node next = null;
}
...
Node head = new Node(); // first Node
head.next = new Node(); // second Node
head.next.next = new Node(); // third node
Node x = new Node(); // node to insert
x.next = head.next.next; //
_____________________ //
head.next.next = x;
Given the following partial code, fill in the blank to complete the code necessary to insert node x after the third node. (don't forget the semicolon)
...
class Node {
public Object data = null;
public Node next = null;
}
...
Node head = new Node(); // first Node
head.next = new Node(); // second Node
head.next.next = new Node(); // third node
Node x = new Node(); // node to insert
_____________________ //
head.next.next .next = x;
Given the following partial code, fill in the blank to complete the code necessary to remove first node. (don't forget the semicolon)
...
class Node {
public Object data = null;
public Node next = null;
}
...
Node head = new Node(); // first Node
head.next = new Node(); // second Node
head.next.next = new Node(); // third node
head.next.next.next = new Node(); // fourth node
_____________________ //
head = head.next;
Given the following partial code, fill in the blank to complete the code necessary to remove the second node. (don't forget the semicolon)
...
class Node {
public Object data = null;
public Node next = null;
}
...
Node head = new Node(); // first Node
head.next = new Node(); // second Node
head.next.next = new Node(); // third node
head.next.next.next = new Node(); // fourth node
_____________________ //
Evaluate the following code to determine what value will be at the front of the queue after the code completes.
MyQueue<Integer> q = new MyQueue<Integer>();
q.add(38); q.add(56); q.add(73);
38
Evaluate the following code to determine what value will be at the back of the queue after the code completes.
MyQueue<Integer> q = new MyQueue<Integer>();
q.add(33); q.add(43); q.add(74);
74
Evaluate the following code to determine the output.
MyStack<Integer> s = new MyStack<Integer>();
s.push(22); s.push(47); s.push(72);
s.pop(); s.pop();
System.out.println(s.pop());
22
Evaluate the following code to determine what value will be at the bottom of the stack after the code completes.
MyStack<Integer> s = new MyStack<Integer>();
s.push(31); s.push(61); s.push(86);
31
Below is an Quicksort algorithm with several pieces missing (represented by XXXXXXXXXX, and the underlined space). Fill in the missing blank (underlined space) with the correct piece of code.
static void quicksort(MyList aList) {
quicksortHelper(XXXXXXXXXXXXXXX, XXXXXXXXXXXXXXX, aList.size() - 1);
}
static void quicksortHelper(MyList aList, int lowIndex, int highIndex) {
if (lowIndex < highIndex)
{
int pIndex = partition(aList, XXXXXXXXXXXXXXX, XXXXXXXXXXXXXXX);
quicksortHelper(aList, XXXXXXXXXXXXXXX, XXXXXXXXXXXXXXX);
quicksortHelper(aList, XXXXXXXXXXXXXXX, _______________);
}
}
static int partition(MyList aList, int low, int high) {
//pick the value at high index as the pivot value
int pivotValue = aList.at(high);
int storeIndex = XXXXXXXXXXXXXXX;
// Compare remaining array elements against pivot value
for (int compareIndex = XXXXXXXXXXXXXXX; compareIndex < XXXXXXXXXXXXXXX; compareIndex++) {
if (aList.at(XXXXXXXXXXXXXXX) <= pivotValue) {
temp = aList.at(compareIndex);
aList.setAt(compareIndex, aList.at(XXXXXXXXXXXXXXX));
aList.setAt(storeIndex, temp);
storeIndex = XXXXXXXXXXXXXXX;
}
}
// Move pivot value to its final place
temp = aList.at(storeIndex);
aList.setAt(storeIndex, aList.at(high));
aList.setAt(high, temp);
// return the index of the pivot value
return XXXXXXXXXXXXXXX;
}
highIndex?
String bar(String s) {
String x = "";
for (int i = 0; i < s.length(); i++)
x =s.charAt(i) + x;
return x;
}
input: “water”
"retaw"
input: “water”
String bar(String s) {
if (s.length() == 0)
return s;
return s.charAt(s.length() - 1) + bar(s.substring(0, s.length() - 1));
}
"retaw"
input: “water”
String bar(String s) {
if (s.length() == 0)
return s;
return bar(s.substring(0, s.length() - 1)) + s.charAt(s.length() - 1);
}
"water"
input: “water”
String bar(String s) {
if (s.length() < 1)
return s;
return s.charAt(0) + bar(s.substring(1));
}
"water"
Which of the following iterative methods is equivalent to this recursive method?
int foo(int n) {
if (n <= 1)
return n;
return n + foo(n – 1);
}
int foo(int n) {
int x = 0;
while (n > 0) {
x += n;
n--;
}
return x;
}
none of these
int foo(int n) {
int x = n;
while (n > 0) {
x += n;
n--;
}
return x;
}
int foo(int n) {
int x = 0;
while (n > 0) {
n--;
x += n;
}
return x;
}
The first one
How many private methods (including constructors) does this class have?
Refer to the following UML Class Diagram when answering this question.
|
|
|
3
How many arguments does the setName method take?
Refer to the following UML Class Diagram when answering this question.
|
|
|
1
If you do not declare a default constructor in your class, the compiler will always create a default constructor for you.
True
False
true
Question 42
The "inherits" keyword is used to specify inheritance in Java.
True
False
true
The Java keyword throw is used to catch an exception.
True
False
false
Any recursive algorithm can be written iteratively.
True
False
false
Evaluate the following code to determine the output.
class Foo {
public int i = 62;
public Foo(int i) {
this.i = i;
}
}
...
Foo x = new Foo(5), y = new Foo(89);
y = x;
y.i = 49;
System.out.println(x.i);
49
Evaluate the following code to determine the output.
class Foo {
public int i = 96;
public Foo(int i) {
this.i = i;
}
}
...
Foo x = new Foo(63), y = new Foo(98);
x.i = y.i;
y.i = 4;
System.out.println(x.i);
98
Binary Tree traversals – consider the following tree:
40
/ \
19 27
/ \ \
32 84 30
/ \
16 23
Show how the tree would be printed using each of the traversals below:
a. Pre-Order: ________________________________________
40, 19, 32, 16, 84, 23, 27, 30
Binary Tree traversals – consider the following tree:
40
/ \
19 27
/ \ \
32 84 30
/ \
16 23
Show how the tree would be printed using each of the traversals below:
In-Order: ______________________________________
16, 32, 19, 84, 23, 40, 27, 30
Binary Tree traversals – consider the following tree:
40
/ \
19 27
/ \ \
32 84 30
/ \
16 23
Show how the tree would be printed using each of the traversals below:
. Post-Order: _______________________________________
16, 32, 23, 84, 19, 30, 27, 40
class TreeNode {
char value;
TreeNode left;
TreeNode right;
public TreeNode(char value) {
this.value = value;
}
}
public static String traverse(TreeNode root) {
if (root == null) return "";
StringBuilder result = new StringBuilder();
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
while (!stack.isEmpty()) {
TreeNode node = stack.pop();
result.append(node.value);
if (node.right != null) {
stack.push(node.right);
}
if (node.left != null) {
stack.push(node.left);
}
}
return result.toString();
}
public static void main(String[] args) {
TreeNode root = new TreeNode('L');
root.left = new TreeNode('M');
root.right = new TreeNode('N');
root.left.left = new TreeNode('O');
root.left.right = new TreeNode('P');
root.right.left = new TreeNode('Q');
System.out.println(traverse(root));
}
LMOPNQ
import java.util.*;
class Node {
int val;
Node left, right;
Node(int val) { this.val = val; }
}
public class BFSOddEvenMagic {
public static void main(String[] args) {
Node root = new Node(19);
root.left = new Node(20);
root.right = new Node(25);
root.right.left = new Node(15);
root.right.right = new Node(10);
bfsMagic(root);
}
static void bfsMagic(Node root) {
Queue<Node> q = new LinkedList<>();
q.add(root);
int level = 0;
int result = 1;
while (!q.isEmpty()) {
int size = q.size();
for (int i = 0; i < size; i++) {
Node node = q.poll();
if ((level % 3 == 1) && (node.val % 5 == 0)) {
result *= node.val;
}
if (node.left != null) q.add(node.left);
if (node.right != null) q.add(node.right);
}
level++;
}
System.out.println(result);
}
}
500
Given that:
int[] list = {4,8,12,18,23,27,33,37,42,46,52,56,61,68,71};
and:
int targetValue = 31;
and given this Binary Search algorithm:
public static int BinarySearch(int[] list, int targetValue) {
int mid, low, high;
low = 0;
high = list.length - 1;
while (high >= low) {
mid = (high + low) / 2;
if (list[mid] < targetValue) {
low = mid + 1;
} else if (list[mid] > targetValue) {
high = mid - 1;
} else {
return mid;
}
}
return -1; // not found
}
18