Podcast
Questions and Answers
What output will the following code produce?
int height = 4; int width = 10; for (int rowCount = 0; rowCount < height; rowCount++ ) { for (int colCount = 0; colCount < width; colCount++ ) { System.out.print("@"); } System.out.println(); }
What output will the following code produce?
int height = 4; int width = 10; for (int rowCount = 0; rowCount < height; rowCount++ ) { for (int colCount = 0; colCount < width; colCount++ ) { System.out.print("@"); } System.out.println(); }
- A 4x10 grid of numbers.
- A single line of 40 '@' symbols.
- Ten lines, each containing four '@' symbols.
- Four lines, each containing ten '@' symbols. (correct)
In a while
loop, under what condition will the code block inside the loop execute?
In a while
loop, under what condition will the code block inside the loop execute?
- When a specific exception is thrown.
- When the boolean expression is true. (correct)
- When the boolean expression is false.
- Only once, at the beginning of the program.
Consider the Java code:
int num = 1; int sum = 0; while (num <= 100) { sum += num; num++; }
What is the final value of the variable sum
?
Consider the Java code:
int num = 1; int sum = 0; while (num <= 100) { sum += num; num++; }
What is the final value of the variable sum
?
- 10000
- 5050 (correct)
- 50
- 100
What is the key difference between a while
loop and a do-while
loop in Java?
What is the key difference between a while
loop and a do-while
loop in Java?
What is the primary purpose of the break
statement in a loop?
What is the primary purpose of the break
statement in a loop?
Given a for
loop that iterates through numbers 1 to 10, what would be the effect of a continue
statement when the loop variable is 5?
Given a for
loop that iterates through numbers 1 to 10, what would be the effect of a continue
statement when the loop variable is 5?
What is the key difference between the break
and continue
statements in loop control?
What is the key difference between the break
and continue
statements in loop control?
Which of the following statements about Java arrays is correct?
Which of the following statements about Java arrays is correct?
What is the purpose of an array in Java?
What is the purpose of an array in Java?
Given the following array declaration, int[] numbers = {10, 20, 30, 40, 50};
, how would you access the third element (value 30) of the array?
Given the following array declaration, int[] numbers = {10, 20, 30, 40, 50};
, how would you access the third element (value 30) of the array?
Which of the following code snippets correctly instantiates an array of integers with a size of 10?
Which of the following code snippets correctly instantiates an array of integers with a size of 10?
If you declare an array as int[] numbers = new int[5];
, what is the initial value of numbers[0]
?
If you declare an array as int[] numbers = new int[5];
, what is the initial value of numbers[0]
?
What is the output of the following code?
int[] arr = {50, 60, 70, 80}; for (int i = 0; i <= arr.length; i++) { System.out.println(arr[i]); }
What is the output of the following code?
int[] arr = {50, 60, 70, 80}; for (int i = 0; i <= arr.length; i++) { System.out.println(arr[i]); }
Given the following code, what will be printed to the console?
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"}; for (String i : cars) { System.out.println(i); }
Given the following code, what will be printed to the console?
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"}; for (String i : cars) { System.out.println(i); }
Consider the code:
int[] ages = {27, 12, 82, 70, 54, 1, 30, 34}; for (int i = 0; i < ages.length; i++ ) { System.out.println("Age is " + ages[1]); }
What will be the output?
Consider the code:
int[] ages = {27, 12, 82, 70, 54, 1, 30, 34}; for (int i = 0; i < ages.length; i++ ) { System.out.println("Age is " + ages[1]); }
What will be the output?
Given the array int[] ages = new int[5];
, what is the result of the following code?
for (int i = 0; i < ages.length; i++ ) { ages[i] = 10; }
Given the array int[] ages = new int[5];
, what is the result of the following code?
for (int i = 0; i < ages.length; i++ ) { ages[i] = 10; }
Given the code: int[] ages = {27, 12, 82, 70, 54, 1, 30, 34};
What is the output of the following enhanced for loop?
for (int age : ages ) { System.out.println("Age is " + age); }
Given the code: int[] ages = {27, 12, 82, 70, 54, 1, 30, 34};
What is the output of the following enhanced for loop?
for (int age : ages ) { System.out.println("Age is " + age); }
In the break example
code, what will happen when a unitScore
is greater than passmark
?
In the break example
code, what will happen when a unitScore
is greater than passmark
?
Referring to the continue example
code, what occurs when score[i]
is less than passMark
?
Referring to the continue example
code, what occurs when score[i]
is less than passMark
?
How would you declare a two-dimensional array named sales
of type double
?
How would you declare a two-dimensional array named sales
of type double
?
How do you instantiate a two-dimensional array named yearlySales
with 5 rows and 4 columns?
How do you instantiate a two-dimensional array named yearlySales
with 5 rows and 4 columns?
Given int[][] sales = new int[3][4];
, how would you initialize the element in the first row and second column to 1000
?
Given int[][] sales = new int[3][4];
, how would you initialize the element in the first row and second column to 1000
?
Which of the following loops will print each element of the 2D array int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
?
Which of the following loops will print each element of the 2D array int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
?
What is the result of running this code?
int[] numbers = {1, 2, 3, 4, 5};
int sum = 0;
for (int i = 0; i < numbers.length; i++) {
if (i % 2 == 0) {
continue;
}
sum += numbers[i];
}
System.out.println(sum);
What is the result of running this code?
int[] numbers = {1, 2, 3, 4, 5};
int sum = 0;
for (int i = 0; i < numbers.length; i++) {
if (i % 2 == 0) {
continue;
}
sum += numbers[i];
}
System.out.println(sum);
Which of the following best describes the ArrayIndexOutOfBoundsException
in Java?
Which of the following best describes the ArrayIndexOutOfBoundsException
in Java?
What is the output after executing the given code snippet?
int[] arr = {10, 5, 8, 20};
int maxVal = arr[0];
for (int num : arr) {
if (num > maxVal) {
maxVal = num;
}
}
System.out.println(maxVal);
What is the output after executing the given code snippet?
int[] arr = {10, 5, 8, 20};
int maxVal = arr[0];
for (int num : arr) {
if (num > maxVal) {
maxVal = num;
}
}
System.out.println(maxVal);
What would be the output of the following program?
public class Main {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
for (int i = 0; i < numbers.length; i++) {
if (numbers[i] == 3) {
break;
}
System.out.println(numbers[i]);
}
}
}
What would be the output of the following program?
public class Main {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
for (int i = 0; i < numbers.length; i++) {
if (numbers[i] == 3) {
break;
}
System.out.println(numbers[i]);
}
}
}
Consider the code:
int[] numbers = {1, 2, 3, 4, 5};
int sum = 0;
for (int number : numbers) {
if (number % 2 == 0) {
continue;
}
sum += number;
}
System.out.println(sum);
What will be printed?
Consider the code:
int[] numbers = {1, 2, 3, 4, 5};
int sum = 0;
for (int number : numbers) {
if (number % 2 == 0) {
continue;
}
sum += number;
}
System.out.println(sum);
What will be printed?
Analyze given code and tell what will be the output:
public class Example {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
for (int i = 0; i <= arr.length; i++) {
try {
System.out.print(arr[i] + " ");
} catch (ArrayIndexOutOfBoundsException e) {
System.out.print("Exception ");
}
}
}
}
Analyze given code and tell what will be the output:
public class Example {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
for (int i = 0; i <= arr.length; i++) {
try {
System.out.print(arr[i] + " ");
} catch (ArrayIndexOutOfBoundsException e) {
System.out.print("Exception ");
}
}
}
}
Suppose you have the following code:
int[][] array2D = {{1, 2}, {3, 4, 5}};
System.out.println(array2D[1].length);
What will be printed to the console?
Suppose you have the following code:
int[][] array2D = {{1, 2}, {3, 4, 5}};
System.out.println(array2D[1].length);
What will be printed to the console?
What is the correct syntax to declare a String
array named names
with a size of 5?
What is the correct syntax to declare a String
array named names
with a size of 5?
What is the output of the following code snippet?
int[] numbers = new int[5];
for (int i = 0; i < numbers.length; i++) {
numbers[i] = i * 2;
}
System.out.println(numbers[3]);
What is the output of the following code snippet?
int[] numbers = new int[5];
for (int i = 0; i < numbers.length; i++) {
numbers[i] = i * 2;
}
System.out.println(numbers[3]);
Given String[][] names = {{"Mr", "Mrs", "Ms"}, {"Smith", "Jones"}};
, what is the value of names[0][1]
?
Given String[][] names = {{"Mr", "Mrs", "Ms"}, {"Smith", "Jones"}};
, what is the value of names[0][1]
?
What will the code System.out.println(numbers.length);
print, given this array: int[] numbers = {1, 2, 3, 4, 5};
?
What will the code System.out.println(numbers.length);
print, given this array: int[] numbers = {1, 2, 3, 4, 5};
?
Given the code:
int[] arr = {1, 2, 3, 4, 5};
for (int i = 0; i < arr.length; i++) {
if (arr[i] % 2 != 0) {
arr[i] = 0;
}
}
What are the contents of arr
after the loop?
Given the code:
int[] arr = {1, 2, 3, 4, 5};
for (int i = 0; i < arr.length; i++) {
if (arr[i] % 2 != 0) {
arr[i] = 0;
}
}
What are the contents of arr
after the loop?
What will the value of sum
be after executing the following code?
int[] nums = {10, 20, 30, 40};
int sum = 0;
for (int i = 1; i < nums.length; i++) {
sum += nums[i];
}
What will the value of sum
be after executing the following code?
int[] nums = {10, 20, 30, 40};
int sum = 0;
for (int i = 1; i < nums.length; i++) {
sum += nums[i];
}
What is the output of this code?
String[] colors = {"red", "green", "blue"};
for (String color : colors) {
System.out.println(color.toUpperCase());
}
What is the output of this code?
String[] colors = {"red", "green", "blue"};
for (String color : colors) {
System.out.println(color.toUpperCase());
}
Given the code:
int[][] matrix = new int[3][3];
int count = 1;
for(int i = 0; i < matrix.length; i++) {
for(int j = 0; j < matrix[i].length; j++) {
matrix[i][j] = count++;
}
}
What is the value of matrix[1][2]
?
Given the code:
int[][] matrix = new int[3][3];
int count = 1;
for(int i = 0; i < matrix.length; i++) {
for(int j = 0; j < matrix[i].length; j++) {
matrix[i][j] = count++;
}
}
What is the value of matrix[1][2]
?
Flashcards
Nested for Loop
Nested for Loop
A loop inside another loop, allowing for more complex iteration patterns.
While Loop
While Loop
A loop that executes a block of code as long as a specified condition is true.
Do-While Loop
Do-While Loop
A loop that executes a block of code once, and then repeats as long as a specified condition is true.
Break Statement
Break Statement
Signup and view all the flashcards
Continue Statement
Continue Statement
Signup and view all the flashcards
Function of 'break'
Function of 'break'
Signup and view all the flashcards
Function of 'continue'
Function of 'continue'
Signup and view all the flashcards
Array
Array
Signup and view all the flashcards
One-Dimensional Array
One-Dimensional Array
Signup and view all the flashcards
Array Index
Array Index
Signup and view all the flashcards
Array Length
Array Length
Signup and view all the flashcards
Traversing an Array
Traversing an Array
Signup and view all the flashcards
ArrayIndexOutOfBoundsException
ArrayIndexOutOfBoundsException
Signup and view all the flashcards
"For-each" Loop
"For-each" Loop
Signup and view all the flashcards
For Loop with Arrays
For Loop with Arrays
Signup and view all the flashcards
Setting Array Values
Setting Array Values
Signup and view all the flashcards
Two-Dimensional Array
Two-Dimensional Array
Signup and view all the flashcards
Declaring a 2D Array
Declaring a 2D Array
Signup and view all the flashcards
Instantiating a 2D Array
Instantiating a 2D Array
Signup and view all the flashcards
Initializing a 2D Array
Initializing a 2D Array
Signup and view all the flashcards
Study Notes
- Lecture 5 by Dr. Dina Saif
Nested for Loop
- The provided code demonstrates a nested for loop in Java.
- The outer loop iterates based on the height, which is set to 4.
- The inner loop iterates based on the width, which is set to 10.
- The inner loop prints "@" without a newline for each column in the current row.
System.out.println()
is called after each inner loop, moving to the next line.
Creating while Loops
- A
while
loop executes a block of code as long as a boolean expression is true. - Syntax of a
while
loop includeswhile (boolean expression) { code block; }
. - The code block is executed repeatedly as long as the boolean expression evaluates to true.
- If the boolean expression is false, the program skips the code block and continues execution after the loop.
While Loop Example
- The
while
loop example uses variable i, initialized to 0. - The loop continues as long as
i
is less than 5. - Inside the loop, the current value of
i
is printed, theni
increments by 1.
Example: NumberAdder Class
- The NumberAdder class provides an example of using a
while
loop. - It calculates the sum of numbers from 1 to 100.
- The loop continues as long as the variable "num" is less than or equal to 100.
- Inside the loop,
num
is added to the value in "sum", and then incrementsnum
by 1. - Finally, it prints the sum of numbers from 1 to 100.
Do-While Loop
- A
do-while
loop executes a block of code at least once, and then continues to execute the block repeatedly if the condition is true. - The code block is executed before the condition is checked.
Even Numbers using For and While Loop
- Demonstrates how to print even numbers from 1 to 100 using both
for
andwhile
loops. - Using
for
loop, anif
statement checks if the current number is even (divisible by 2). - Using
while
loop also uses anif
statement to check for evenness and prints accordingly
Descending Numbers Program
- Program prints numbers from 50 to 10 in descending order, using both
for
andwhile
loops. - The
for
loop initializes i to 50, continues as long as i is greater than or equal to 10, and decrements i by 1 in each iteration. - The
while
loop initializes j to 50, continues as long as j is greater than or equal to 10, and decrements j by 1 in each iteration.
Break Statement
- A break statement is used to exit a loop early.
- The Java code demonstrates how to print numbers from 1 to 10, but stop when the number 5 is reached using a break statement.
Break Example Program
- The loop prints numbers from 1 to 4, then exits when it reaches the number 5.
Continue Statement
- The
continue
statement skips the current iteration and goes to the next iteration of the loop. - Used to print numbers from 1 to 10, skipping the number 5.
Continue Example Program
- Demonstrates skipping the number 5 within the loop, printing all other numbers from 1 to 10.
Break and Continue Statements
Break
statement terminates the entire loop and jumps to the statement after it.Continue
statement skips the current iteration and proceeds to the next iteration of the loop.- Using both can make code flexible and efficient by controlling loop flow based on conditions.
Introduction to Arrays
- An array is a container object that holds a group of values of a single type.
- The type of the values in the array can be a primitive or an object type.
- The length of an array is established when the array is created and fixed after.
- Each item in an array is called an element, accessed by a numerical index, which starts at 0 (zero).
One-Dimensional Arrays
- Example declaration of multiple one dimensional arrays
- Each array is of type
int
Creating One-Dimensional Arrays
- Depicts arrays of
int
types and arrays ofString
types
Multidimensional Arrays
- Multidimensional arrays will have rows and columns.
- This can be thought of as a grid.
Array length is 8
- First index starts at 0
- Last index is element 5 at the value 1
- Ages length is 8
(ages.length)
Java Array Advantages
- Code Optimization: optimizes code by retrieving and sorting efficiently.
- Random access: accesses any data located at an index position.
- Disadvantage: has a size limit
Single Dimensional Array in Java
dataType[] arr;
ordataType []arr;
ordataType arr[];
arrayRefVar=new datatype[size];
Storing Arrays in Memory
char size = 'L'
char[] sizes = {'S', 'M', 'L' };
- Shows how primitive variables are stored in memory
Storing Arrays of References in Memory
Shirt myShirt = new Shirt();
Shirt[] shirts = { new Shirt(), new Shirt(), new Shirt() };
- Shows how shirts are stored in memory
Traversing Array
- Traverses an array and prints all numbers from 10, 20, 70, 40, and 50 through each loops
Declaration and Initialization
- Declares variables to 33, 3, 4, 5 and prints each line
ArrayIndexOutOfBoundsException
- An exception that will occur if the bounds of an array is broken
For Each Array
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
- Prints each car
For Loop with Arrays
- Each array item is printed out
- "Age is..." ages is the array being used
Set Values in Arrays
- Each element in array is set to the value 10
Enhanced for Loop with Arrays
- Array is transversed and printed in each line
Enhanced for Loop with Array Lists
- Code loops through an ArrayList to access each element
Using Break with Loops
- If you meet condition, end loop.
- There is no need to go through loop so break.
Using Continue with Loops
- If failed unit, test for all next unit
- If unit failed, go on to check the next unit
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.