Skip to main content
Contents
Dark Mode Prev Up Next
\(\newcommand{\N}{\mathbb N} \newcommand{\Z}{\mathbb Z} \newcommand{\Q}{\mathbb Q} \newcommand{\R}{\mathbb R}
\newcommand{\lt}{<}
\newcommand{\gt}{>}
\newcommand{\amp}{&}
\definecolor{fillinmathshade}{gray}{0.9}
\newcommand{\fillinmath}[1]{\mathchoice{\colorbox{fillinmathshade}{$\displaystyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\textstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptscriptstyle\phantom{\,#1\,}$}}}
\)
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 . Click on a word to "select" it. Make sure to select the parameters both in the function definition and whenever they are used in the function body!
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 variables .
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);
}
}
}