Section 2.3 Exercises: Identifying variables
These practice problems should be done alongside the Java Basics reading.
Before attempting these problems, make sure you have read about the different variable types.
Exercises Identifying variable types in code
1. Identifying parameters.
In the following code chunk, locate all the names that are parameters.
public class LList {
private Node head = null;
public void insertFront(int value) {
head = new Node(value, head);
}
public boolean contains(int value) {
Node curr = head;
while (curr != null) {
if (curr.value == value) return true;
curr = curr.next;
}
return false;
}
private static class Node {
private int value;
private Node next;
Node(int value, Node next) {
this.value = value;
this.next = next;
}
Node(int value) {
this(value, null);
}
}
}
2. Identifying local variables.
In the following code chunk, locate all the names that are local parameters.
public class LList {
private Node head = null;
public void insertFront(int value) {
head = new Node(value, head);
}
public boolean contains(int value) {
Node curr = head;
while (curr != null) {
if (curr.value == value) return true;
curr = curr.next;
}
return false;
}
private static class Node {
private int value;
private Node next;
Node(int value, Node next) {
this.value = value;
this.next = next;
}
Node(int value) {
this(value, null);
}
}
}
3. Identifying fields.
In the following code chunk, locate all the names that are fields (member variables in C++ language).
public class LList {
private Node head = null;
public void insertFront(int value) {
head = new Node(value, head);
}
public boolean contains(int value) {
Node curr = head;
while (curr != null) {
if (curr.value == value) return true;
curr = curr.next;
}
return false;
}
private static class Node {
private int value;
private Node next;
Node(int value, Node next) {
this.value = value;
this.next = next;
}
Node(int value) {
this(value, null);
}
}
}