IT401 Business Computer Languages Instructions: Name: ### CRN: ### ID: ### 2 Mar
ID: 3829601 • Letter: I
Question
IT401
Business Computer Languages
Instructions:
Name: ###
CRN: ###
ID: ###
2 Marks
Learning Outcome(s):
Ch. (6).
Be able to use common loop algorithms
Be able to program loops with the while and for statements
Question One
Write a program in Java to read 5 integers and print their sum and average. Use loop statements in your code. The output of your program looks like:
1.5 Marks
Learning Outcome(s):
Ch. (6).
Be able to use common loop algorithms
Be able to program loops with the while and for statements
Question Two
Write a program in Java to read 5 integers and print and print the minimum and maximum values .Use while loop and if statements in your code. The output of your program looks like:
0.5 Marks
Learning Outcome(s):
Ch. (7)
become familiar with using arrays and array lists
Question Three
What is the value of m after running the following code?
int[] m = new int[8];
m[0] = 1;
for (int i = 1; i < m.length; i++) {
m[i] = m[i-1] + i;
}
1 Marks
Learning Outcome(s):
Ch. (7)
Become familiar with using arrays and array lists
Question Four
Find and fix the error in each of the following codes:
1.
int[ ] a = new int[ 60 ];
for( int h = 0; h <= a.length; h++ )
a[ h ] = 1;
2.
ArrayList m = new ArrayList();
m.add(1);
m.add(2);
m.remove(0);
Explanation / Answer
1. Write a program in Java to read 5 integers and print their sum and average. Use loop statements in your code. The output of your program looks like:
import java.io.*;
import java.util.*;
class Question 1 {
public static void main(String args[]) throws IOException {
Scanner obj = new Scanner(System.in);
int sum=0;
for(int i=0;i<5;i++){
sum += obj.nextInt();
}
double avg = (sum*1d)/5;
System.out.println("Sum : "+sum+" Avg : "+avg);
}
}
OUTPUT :
2
1
5
4
3
Sum : 15 Avg : 3.0
2.. Question 2 :
import java.io.*;
import java.util.*;
class Question2 {
public static void main(String args[]) throws IOException {
Scanner obj = new Scanner(System.in);
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
for(int i=0;i<5;i++){
int num = obj.nextInt();
min = Math.min(min, num);
max = Math.max(max, num);
}
System.out.println("Min : "+min+" Max : "+max);
}
}
OUTPUT :
2
3
1
5
4
Min : 1 Max : 5
3. Question 3 ;
Array m is [1, 2, 4, 7, 11, 16, 22, 29]
4. Question 4
The terminating condition for the for loop is h<=a.length. This will give ArrayIndexOutofBoundsException since h can be atmax a.length-1;
In this ArrayList, the object types which the arraylist holds is not declared. But, this won't cause any error.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.