Docsity

Java Programming Midterm II Practice Guide, Quizzes of Java Programming

Toggle sidebar
bg1
CSE 1223 Intro to Computer Programming in Java
Midterm II Practice Guide 2025-2026
1
/
9
1.
Declaring an array
double
[]
temperature;
int [] scores;
boolean [] answers;
String [] names;
2.
Allocating an array
temperatures
=
new
double
[10];
scores = new int [20];
answers = new boolean [5];
names = new String [15];
3.
Accessing array ele-
ments
4.
how do you find the
number of elements in
an array
5.
Declare and allocate an
array of 10 integers and
call it a
6.
Write a loop that initial-
izes the 10 elements of
the array to the integers
1, 2, 3, 4, 5, 6, 7, 8, 9,
10
temperatures
[2];
temperatures.length;
int
[]
a
=
new
int
[10];
int
[]
a
=
new
int
[10];
for (int i = 1; i < a.length; i++){
a[i] = i+1;
}
7.
Write
a
loop
that
outputs
for (int k = 0; k < a.length; k++){
the elements of the
array, one per line, in re-
verse order
System.out.println(a[(a.length-1)-k]);
}
the
last
element
is
a[(a.length
-
1)]
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Java Programming Midterm II Practice Guide and more Quizzes Java Programming in PDF only on Docsity!

CSE 1223 Intro to Computer Programming in Java

Midterm II Practice Guide 2025- 2026

  1. Declaring an array double [] temperature; int [] scores; boolean [] answers; String [] names;
  2. Allocating an array temperatures = new double [10]; scores = new int [20]; answers = new boolean [5]; names = new String [15];
  3. Accessing array ele- ments
  4. how do you find the number of elements in an array
  5. Declare and allocate an array of 10 integers and call it a
  6. Write a loop that initial- izes the 10 elements of the array to the integers 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

temperatures [2];

temperatures.length;

int [] a = new int [10];

int [] a = new int [10]; for (int i = 1; i < a.length; i++){ a[i] = i+1; }

  1. Write a loop that outputs for (int k = 0; k < a.length; k++){ the elements of the array, one per line, in re- verse order

System.out.println(a[(a.length-1)-k]); }

the last element is a[(a.length - 1)]

CSE 1223 Intro to Computer Programming in Java

Midterm II Practice Guide 2025- 2026

  1. Write a program segment—using for loops— that given two integer variables, width and height, outputs a rectan- gle of '+'s of the given width and height.

length is 10 last element index is 9

private static void printRectangle (int w, int h){ for (int i = 0; i<h; i++){ for (int j = 0; j<w; j++){ System.out.print('+'); } System.out.println(); } }

  1. Arrays in java are objects
  2. Arrays hold a reference to an array object
  3. Declaring an array list ArrayList stringList = new ArrayList();
  4. We want code that reads import java.util.ArrayList; in text one line at a time until the user enters the word "stop" Then the code should re- peat back all of the lines in reverse order We do not know how many lines of text there will be before we start

public static void main(String [] args) { ArrayList stringList = new ArrayList(); Scanner keyboard = new Scanner(System.in); String input = ""; while (!input.equals("stop")) { input = keyboard.nextLine(); stringList.add(input); } int i = stringList.size()-1; while (i>0) { System.out.println(stringList.get(i)); i = i - 1;

CSE 1223 Intro to Computer Programming in Java

Midterm II Practice Guide 2025- 2026

  1. Write a segment of code that takes an ArrayList of Strings named list and removes any Strings that are longer than 5 characters
  2. Array Lists cannot hold primitive types, how are they declared then? (wrapper class)

int i=0; while (i<list.size()) { String current = list.get(i); if (current.length()>5) { list.remove(i); } else { i = i + 1; } }

int ’class Integer char ’class Character double ’class Double

  1. boxing Taking a primitive data element and wrapping it up in a wrapper class is known as boxing ex. Double myDouble = new Double(6.0);
  2. unboxing Taking the primitive out of the wrapper is known as unboxing ex. double myPrimitive = myDouble.doubleValue();
  3. autoboxing Java will (usually) automatically do the right thing on its own
  4. Declares a new ArrayList of integers named nums Puts the even numbers from 2 through 20 into this list

solution

  1. Uses an ArrayList of In- tegers named list1 to build a new ArrayList of Inte- gers named list The elements of list should be in reverse or- der from the elements that are in list
  2. how to pass an array list

solution

private static ArrayList to a method and return it copyList(ArrayList al)

  1. Converting String Values to int and double
  2. Write a program that asks the user for a file name, opens the file, reads a bunch of integer values, one per line, until it reaches the end of the file. The program computes and outputs the average of the integers to the screen. Make sure you catch the possible IOException in a try-catch statement

int Integer.parseInt(String str) double Double.parseDouble(String str)

solution

ArrayList l2 = l1; l2.add(0,2); System.out.print- ln(l1.get(0)); // reports 2 System.out.print- ln(l2.get(0)); // reports 2 Why the "strange" be- havior?

  1. why can't we use == to test for String equality?

into the variable l Both of these variables now reference (or "point to") the same mem- ory location We call this aliasing or making an alias. Both l1 and l2 are now two ditterent names for the same object. Just like "Superman" and "Clark Kent" are both names for the same person - they're both aliases to the same object.

Strings are a reference type. We need to use .equals() for Strings We do the same to test equality of other objects (including ArrayLists)

  1. class A class is a definition of a data type Similar to a blueprint for a house
  2. object An object is the data value instantiated (i.e. created) from a class Similar to a physical house built from a blueprint
  3. Why would we want to use an object-oriented programming frame- work?

Code re-use OOP makes it very easy to re-use code from project to project. With a focus on making everything a class, and breaking models up into data and behavior, code reuse comes naturally

Testing OOP makes it much easier to test code to make sure that it works properly Test behaviors of individual objects in isolation, rather than

trying to figure out how to test all of the code at once.

Maintainability OOP makes it easy to find and fix errors in code Error will show up as a problem with the behavior of an object, which narrows down the places to look to find why the object is misbehaving.

Problem Solving OOP models match up to our models of the world very well Everything is an object This makes coming up with solutions to problems in code somewhat easier At least as a starting point

  1. objects have state and behavior Each object has a set of associated data values. The configuration of these values determines its state Each object also has a set of associated methods. These methods define its behavior
  2. encapsulation Meaning "putting things into a capsule (container)" We stutt all of these things (methods & data) into one container - that's an object
  3. Java Strings are an exam- B) an object ple of Provide both data and behavior (string methods)
  4. Java ArrayLists are an ex- B) an object ample of
  5. Java Scanner is an exam- ple of

Provide both data and behavior (ArrayList methods)

B) an object