wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

IP MIDTERM

Total questions: 45

Worksheet time: 23mins

Name
Class
Date
1.
An array is a list of data items that __________.
a)
all have the same type
b)
all have different names
c)
all are integers
d)
all are null
2.
Which of the following can be used as an array subscript?
a)
character
b)
double
c)
int
d)
String
3.
Assume an array is declared as follows. Which of the following statements correctly assigns the value 100 to each of the array elements? int[] num = new int[4];
a)
for(x=0; x<3; ++x) num[x] = 100;
b)
for(x=0; x<4; ++x) num[x] = 100;
c)
for(x=1; x<4; ++x) num[x] = 100;
d)
for(x=1; x<3; ++x) num[x] = 100;
4.
Suppose you declare an array as follows: int[] creditScores = {670,720,815} What is the value of creditScores.lenght?
a)
0
b)
1.0
c)
2.0
d)
3.0
5.
If a class named Student contains a method setID() that takes an int argument and you write an application in which you create an array of 20 Student objects named scholar. Which of the following statements correctly assigns an ID number to the first Student scholar?
a)
Student[0].setID(1234);
b)
scholar[0].setID(1234);
c)
Student.setID[0].(1234);
d)
scholar.setID[0](1234);
6.
The following defines a ____ array. Int[][] nums = {{1,2} ,{3,4},{5,6}};
a)
one dimensional
b)
two dimensional
c)
three dimensional
d)
six dimensional
7.
How many rows are contained in the following array? double [][] prices = {{2.56, 3.57, 4.58, 5.59}, {12.35, 13.35, 14.35, 15.00}};
a)
1.0
b)
2.0
c)
4.0
d)
8.0
8.
What is the difference between a multidimensional array and an ArrayList of arrays in Java?
a)
A multidimensional array can only hold a single data type, while an ArrayList of arrays can hold multiple data types
b)
A multidimensional array has a fixed size, while an ArrayList of arrays can grow or shrink dynamically
c)
A multidimensional array is a primitive type, while an ArrayList of arrays is a data structure
d)
There is no difference between the two
9.
How do you check if an ArrayList is empty in Java?
a)
myArrayList.isEmpty();
b)
myArrayList.size() == 0;
c)
myArrayList.size() == null;
d)
myArrayList == null;
10.
What is an ArrayList in Java?
a)
A fixed-size array that can store elements of different data types
b)
A dynamic array that can grow or shrink in size and store elements of the same data type
c)
A collection of key-value pairs that can be accessed by their keys
d)
A data structure that represents a sequence of elements that can be accessed by their index numbers
11.
What is the Arrays.sort() method used for in Java?
a)
To sort a one-dimensional array in ascending order
b)
To sort a multidimensional array in descending order
c)
To sort an ArrayList in ascending order
d)
To sort a String in descending order
12.
What is the StringBuilder class used for in Java?
a)
To create immutable Strings
b)
To create mutable Strings
c)
To create ArrayLists
d)
To create one-dimensional arrays
13.
What is the indexOf() method used for in Java?
a)
To find the number of characters in a String
b)
To find the index of a specified character or substring in a String
c)
To convert a String to uppercase
d)
To replace a character or substring in a String
14.
How do you convert a String to lowercase in Java?
a)
myString.toLowerCase();
b)
myString.toLowercase();
c)
myString.convertToLower();
d)
myString.convertCase("lower");
15.
What is the replace() method used for in Java?
a)
To find the number of characters in a String
b)
To find the index of a specified character in a String
c)
To replace a character or substring in a String
d)
To split a String into multiple substrings
16.
How do you remove whitespace from the beginning and end of a String in Java?
a)
myString.trim();
b)
myString.removeWhitespace();
c)
myString.deleteWhitespace();
d)
myString.cleanWhitespace();
17.
What is the format() method used for in Java?
a)
To format numerical values in a specific way
b)
To format a String in a specific way
c)
To format the output of a program
d)
To format the input of a program
18.
How do you convert a String to a char array in Java?
a)
myCharArray = myString.toChar();
b)
myCharArray = myString.toCharArray();
c)
myCharArray = CharArray.valueOf(myString);
d)
myCharArray = String.toChar(myString);
19.
What is the compareTo() method used for in Java?
a)
To compare the values of two Strings
b)
To compare the values of two arrays
c)
To compare the values of two ArrayLists
d)
To compare the values of two multidimensional arrays
20.
How do you check if a String contains a specific character or substring in Java?
a)
myString.contains(myChar);
b)
myString.containsSubstring(mySubstring);
c)
myString.indexOf(myChar);
d)
myString.indexOf(mySubstring);
21.
A double type is an example of a primitive data type.
a)
True
b)
false
22.
A one-dimensional array is an example of a structured data type.
a)
True
b)
false
23.
Arrays can be passed as parameters to a method.
a)
True
b)
false
24.
A method can return a value of the type array.
a)
True
b)
false
25.
The size of an array is determined at compile time.
a)
True
b)
false
26.
Given the declaration: int[] list = new int[10]; the statement: list[5] = list[3] + list[2]; updates the content of the fifth element of the array list.
a)
True
b)
false
27.
If an array index goes out of bounds, the program terminates in an error.
a)
True
b)
false
28.
Its easier to fix is logic error rather that syntax error.
a)
True
b)
false
29.
String comparison in Java should always be done using the == operator rather than the equals() method.
a)
True
b)
false
30.
Java's String class is immutable, meaning that the value of a String object cannot be changed once it is created.
a)
True
b)
false
31.

Which of the following method calls is valid?

c = sum(a, b);

a)
VALID
b)
NOT VALID
32.

Which of the following method calls is valid?

a = sum(list1[0], list2[0]);

a)
VALID
b)
NOT VALID
33.

Which of the following method calls is valid?

c = sum(list1, list2);

a)
VALID
b)
NOT VALID
34.

Which of the following method calls is valid?

for (int i = 1; i <= 10; i++) list3[i] = sum(list1[i], list2[i]);

a)
VALID
b)
NOT VALID
35.

Which of the following method calls is valid?

b=sum(list1[a],list[b]);

a)
VALID
b)
NOT VALID
36.

Creating a 1D array of integers and initializing it:

int[] arr = (a)   int[]{1, 2, 3, 4, 5};

37.

Creating a 2D array of strings and initializing it:

String (a)   arr = new String[][]{

{John, Doe},

{Jane, Doe},

{Bob, Smith}};

38.

Accessing an element of a 1D array by index:

int[] arr = new int[]{1, 2, 3, 4, 5};

int value = arr[ (a)   ]; // value will be 3

39.

Accessing an element of a 2D array by row and column index:

String[ ][ ] arr = new String[ ][ ]

{"John", "Doe"},

{"Jane", "Doe"},

{"Bob", "Smith"} };

String value = arr (a)   ; // value will be "Jane"

40.

Usage of foreach loop in java

String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};

for (String i : cars) {

System.out.println( (a)   ); //display all the values of cars }

41.

Creating an ArrayList of integers and adding elements to it:

(a)   <Integer> list = new ArrayList<Integer>();

list.add(1);

list.add(2);

list.add(3);

42.

Creating an ArrayList of strings and adding elements to it:

ArrayList<String> list = new ArrayList<String>();

list.________________("apple");

list.________________("banana");

list.________________("cherry");

(a)  

43.

Removing an element from an ArrayList by index:

ArrayList<String> list = new ArrayList<String>();

list.add("apple");

list.add("banana");

list.add("cherry");

list.remove( (a)   ); // removes the element at index 1 (banana)

44.

Checking if a String contains a substring:

String str = "Hello, world!";

boolean containsSubstring = str. (a)   ("world"); // containsSubstring will be true

45.

Concatenating two strings:

String str1 = "Hello";

String str2 = "world";

String result = (a)   + “ “ + str2; // result will be "Hello world”