Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

[Inst. 4.2] Use the below mentioned code for following question(s). Scanner keyb

ID: 3745164 • Letter: #

Question

[Inst. 4.2] Use the below mentioned code for following question(s).

Scanner keyboard = new Scanner(System.in);

int studentID, studentCount = 0, studentGrade, numberOfStudentGrades = 0, totalOfGrades = 0;
int totalOfStudentGrades = 0, totalNumberOfGrades = 0;
double studentAverage, classAverage;
System.out.println("Enter student ID: ");
studentID = keyboard.nextInt();
while (studentID != 0){
studentCount++;
System.out.println("Enter student grade: :");
studentGrade = keyboard.nextInt();
while(studentGrade != -1)
{
numberOfStudentGrades++;
totalOfStudentGrades += studentGrade;
System.out.println("Enter student grade: :");
studentGrade = keyboard.nextInt();
}
if (numberOfStudentGrades > 0)
{
studentAverage = (double)totalOfStudentGrades/numberOfStudentGrades;
System.out.println("Student " + studentID + "average is " + studentAverage);
totalNumberOfGrades += numberOfStudentGrades;
totalOfGrades += totalOfStudentGrades;
}
numberOfStudentGrades = 0;
totalOfStudentGrades = 0;
System.out.println("Enter student ID: ");
studentID = keyboard.nextInt();
}
if (totalNumberOfGrades != 0)
System.out.println("Average of all grades is " + (totalOfGrades/totalNumberOfGrades));


Refer to Inst. 4.2. If there are 8 students and they have taken 8, 10, 5, 3, 7, 11, 12, and 9 tests respectively, how many times will the inner loop execute for all students?

[Inst. 4.2] Use the below mentioned code for following question(s).

Scanner keyboard = new Scanner(System.in);

int studentID, studentCount = 0, studentGrade, numberOfStudentGrades = 0, totalOfGrades = 0;
int totalOfStudentGrades = 0, totalNumberOfGrades = 0;
double studentAverage, classAverage;
System.out.println("Enter student ID: ");
studentID = keyboard.nextInt();
while (studentID != 0){
studentCount++;
System.out.println("Enter student grade: :");
studentGrade = keyboard.nextInt();
while(studentGrade != -1)
{
numberOfStudentGrades++;
totalOfStudentGrades += studentGrade;
System.out.println("Enter student grade: :");
studentGrade = keyboard.nextInt();
}
if (numberOfStudentGrades > 0)
{
studentAverage = (double)totalOfStudentGrades/numberOfStudentGrades;
System.out.println("Student " + studentID + "average is " + studentAverage);
totalNumberOfGrades += numberOfStudentGrades;
totalOfGrades += totalOfStudentGrades;
}
numberOfStudentGrades = 0;
totalOfStudentGrades = 0;
System.out.println("Enter student ID: ");
studentID = keyboard.nextInt();
}
if (totalNumberOfGrades != 0)
System.out.println("Average of all grades is " + (totalOfGrades/totalNumberOfGrades));


Refer to Inst. 4.2. If there are 5 students and each student has 10 grades, how many times will the inner loop execute for each student?

[Inst. 4.2] Use the below mentioned code for following question(s).

Scanner keyboard = new Scanner(System.in);

int studentID, studentCount = 0, studentGrade, numberOfStudentGrades = 0, totalOfGrades = 0;
int totalOfStudentGrades = 0, totalNumberOfGrades = 0;
double studentAverage, classAverage;
System.out.println("Enter student ID: ");
studentID = keyboard.nextInt();
while (studentID != 0){
studentCount++;
System.out.println("Enter student grade: :");
studentGrade = keyboard.nextInt();
while(studentGrade != -1)
{
numberOfStudentGrades++;
totalOfStudentGrades += studentGrade;
System.out.println("Enter student grade: :");
studentGrade = keyboard.nextInt();
}
if (numberOfStudentGrades > 0)
{
studentAverage = (double)totalOfStudentGrades/numberOfStudentGrades;
System.out.println("Student " + studentID + "average is " + studentAverage);
totalNumberOfGrades += numberOfStudentGrades;
totalOfGrades += totalOfStudentGrades;
}
numberOfStudentGrades = 0;
totalOfStudentGrades = 0;
System.out.println("Enter student ID: ");
studentID = keyboard.nextInt();
}
if (totalNumberOfGrades != 0)
System.out.println("Average of all grades is " + (totalOfGrades/totalNumberOfGrades));


Refer to Inst. 4.2. If 10 students are entered and each student has 12 grades, how many times does the outer loop execute?

Cannot tell

[Inst. 4.2] Use the below mentioned code for following question(s).

Scanner keyboard = new Scanner(System.in);

int studentID, studentCount = 0, studentGrade, numberOfStudentGrades = 0, totalOfGrades = 0;
int totalOfStudentGrades = 0, totalNumberOfGrades = 0;
double studentAverage, classAverage;
System.out.println("Enter student ID: ");
studentID = keyboard.nextInt();
while (studentID != 0){
studentCount++;
System.out.println("Enter student grade: :");
studentGrade = keyboard.nextInt();
while(studentGrade != -1)
{
numberOfStudentGrades++;
totalOfStudentGrades += studentGrade;
System.out.println("Enter student grade: :");
studentGrade = keyboard.nextInt();
}
if (numberOfStudentGrades > 0)
{
studentAverage = (double)totalOfStudentGrades/numberOfStudentGrades;
System.out.println("Student " + studentID + "average is " + studentAverage);
totalNumberOfGrades += numberOfStudentGrades;
totalOfGrades += totalOfStudentGrades;
}
numberOfStudentGrades = 0;
totalOfStudentGrades = 0;
System.out.println("Enter student ID: ");
studentID = keyboard.nextInt();
}
if (totalNumberOfGrades != 0)
System.out.println("Average of all grades is " + (totalOfGrades/totalNumberOfGrades));


Refer to Inst. 4.2. The if (totalNumberOfGrades != 0)

D. Ensures that each grade is greater than 0

[Inst. 4.2] Use the below mentioned code for following question(s).

Scanner keyboard = new Scanner(System.in);
int studentID, studentCount = 0, studentGrade, numberOfStudentGrades = 0, totalOfGrades = 0;
int totalOfStudentGrades = 0, totalNumberOfGrades = 0;
double studentAverage, classAverage;
System.out.println("Enter student ID: ");
studentID = keyboard.nextInt();
while (studentID != 0){
studentCount++;
System.out.println("Enter student grade: :");
studentGrade = keyboard.nextInt();
while(studentGrade != -1)
{
numberOfStudentGrades++;
totalOfStudentGrades += studentGrade;
System.out.println("Enter student grade: :");
studentGrade = keyboard.nextInt();
}
if (numberOfStudentGrades > 0)
{
studentAverage = (double)totalOfStudentGrades/numberOfStudentGrades;
System.out.println("Student " + studentID + "average is " + studentAverage);
totalNumberOfGrades += numberOfStudentGrades;
totalOfGrades += totalOfStudentGrades;
}
numberOfStudentGrades = 0;
totalOfStudentGrades = 0;
System.out.println("Enter student ID: ");
studentID = keyboard.nextInt();
}
if (totalNumberOfGrades != 0)
System.out.println("Average of all grades is " + (totalOfGrades/totalNumberOfGrades));


Refer to Inst. 4.2. In the while (studentID != 0)) statement, what is the function of "0"?

A. 8

Explanation / Answer


Ques1:
Answer :The while loop i.e. while(studentGrade != -1)executes infinitely.
Explanation:
=>Since the studentid is got from keyboard, once we enter the studentid, the first while loop checks if it is not equal to zero.
=>And enters the loop and executes infinite times the inner loop containing another while asking for the student grade.
Conclusion: D.Cannot tell
Ques2:
Answer:The while loop i.e. while(studentGrade != -1)executes infinitely
Explanation:
=>Since the studentid is got from keyboard, once we enter the studentid, the first while loop checks if it is not equal to zero.
=>And enters the loop and executes infinite times the inner loop containing another while asking for the student grade.
=>Irrespective of the number of students, for first student itself the loop executes infinite times.
Conclusion: D.Cannot tell

Ques3:
Given: There are 10 students.
Assumption:1.So there will be 10 studentids.
2.But here the inner loop doesnt exit and executes infinitely for the first studentid itself.

The outer loop should execute 10 times since there are 10 studentids.
Answer: But the above is possible only if the inner loop exists. So outer loop executes 10 times.
Conclusion: According to given code, D.Cannot Tell.

Ques4:
Given : if(totalNumberOfGrades!=0)

Ensures the totalNumberOfGrades is not 0 if yes then the average is calculated.
Answer: D. Ensures that each grade is greater than 0

Ques5:

In the while (studentID != 0)) statement, what is the function of "0"

Explanation:

=>Here 0 is used as a sentinel. i.e it is used as a stopping value or used like a condition of termination.

=>while the studentID values are not equal to 0, the loop is executed.

Answer: C. It is a sentinel

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote