Iteration in Programming
Which method would you use to determine the number of elements in an ArrayList named 'myList'?
myList.count()
myList.size()
myList.capacity()
myList.length()
If you need to ensure that an instance of your 'House' class always has an 'Address', what technique would best enforce this relationship without inheriting from 'Address'?
Static binding where House statically imports all functionalities from Address into itself.
Inheritance by making House a subclass of Address to inherit its properties.
Interface implementation where House implements an Addressable interface but does not contain an Address.
Composition by having an Address instance as part of every House object's state.
What happens to the value of "i" after each iteration in a standard for loop structure such as for(int i = 0; i < 10; i++)
?
It remains constant.
It decreases by 1.
It is multiplied by 2.
It increases by 1.
Given this code segment:
java
int result =0;
for (int j=10; j>0;j--){
if(j%3==0){
result+=j;
}
}
Which change will make result
hold double its original final value after execution?
Replace result+=j;
with result+=j*2;
.
Remove j--
.
Change the initialization to int j=20;
.
Replace if(j%3==0)
with if(j%6==0)
.
What type of loop should you use when you need to repeat an action a fixed number of times?
Do-while loop
Switch loop
While loop
For loop
When would you choose a while loop over a for loop?
When the number of repetitions isn't known
When you want to do repeated computations
When you need to iterate over a finite number of objects
When you want to simplify code structure
What is the time complexity of a nested for loop where the outer loop runs n times and the inner loop runs n/2 times?
O(n log n)
O(n^2)
O(n)
O(log n)

How are we doing?
Give us your feedback and let us know how we can improve
In Java, which of these data types can store decimal numbers?
char
double
int
boolean
What will the following code output:
for (int i = 0; i < 5; i++) { System.out.print(i + " "); }
1 2 3 4
1 2 3 4 5
0 1 2 3 4 5
0 1 2 3 4
In a decrementing for-loop structured as
java
for(int x=n;x>0;x--){
//body //
}```
Which modification ensures that exactly half the numbers from N down to zero are processed?
Set initialization to int x = n/2
.
Change the condition to x > n - ((n+1)/2)
.
Add x /= 2
at the start of the loop body.
Change loop condition to x > N/2 && x--
.