Question & Answer: 6. What is the result? public static void main(Strigargs) for () t to Java Program prints "Welcome to Java" an infinite number of times. O Com…..

6. What is the result? public static void main(Strigargs) for () t to Java Program prints Welcome to Java an infinite number of times. O Compilation error as expressions are missing in the for loop. O Program prints Welcome to Java once. No error and no output. 7. You want to compute the sum of all the marks of a given subject. Which approach will you choose? if statement O switch statement O if/else statement ○ Looping 8. The for loop provides a complicated way to iterate over a range of values True False 9. When is an update expression in a for loop executed? Before each iteration through the loop. OAfter bwo iterations through the loop. 0 Before the first iteration through the loop. O After each iteration through the loop. er all questions in this section) 10. Access and visibility of a class should be limited as much as possible. ○ False

6. What is the result? public static void main(Strigargs) for () t to Java Program prints “Welcome to Java” an infinite number of times. O Compilation error as expressions are missing in the for loop. O Program prints “Welcome to Java” once. No error and no output. 7. You want to compute the sum of all the marks of a given subject. Which approach will you choose? if statement O switch statement O if/else statement ○ Looping 8. The for loop provides a complicated way to iterate over a range of values True False 9. When is an update expression in a for loop executed? Before each iteration through the loop. OAfter bwo iterations through the loop. 0 Before the first iteration through the loop. O After each iteration through the loop. er all questions in this section) 10. Access and visibility of a class should be limited as much as possible. ○ False

Expert Answer

 

Answers are as follows with required explanation for every possible question::

Question 6:

  • Program prints “Welcome to Java” an infinite number of times.
  • for loop has four components with it and they are for(Initialization ;condition ;update) and body of loop.
  • Initilization executes only once at first loop of the for loop. Condition is boolean parameter and checks if given parameter holds certain condition or no for example i<10.
  • After condition check , loop executes its body and go for update.
  • Now for(;;) has no initialization which is allowed in java and in condition too there is nothing and by default this condition is taken as true always. There no update either.
  • So loop runs forever i.e for infinite time.

Question 7:

  • Answer is (d) Looping.
  • To compute sum of all the marks of a given student we need to have datastructure like Array.
  • So we need to traverse through array to calculate easc subject marks.
  • if statement is used just to check a particular condition holds TRUE or FALSE.
  • if/else statement is also similar to if statement. But it is used to execute one of the option like if a<10 then do a++ else do a–.
  • Switch statement is used for execution of cases. It can have many cases from which only one gets executed.
  • Looping for loop is generally used to traverse through array.

Question 8:

  • Answer is FALSE.
  • for loop is very simple for iterating/traversing over a range of values.
  • For example consider there is an array of size 100 and it has integer values stored in it and our job is to get sum of values in range of index starting at 5 to 60(both index inclusive).
  • Then for loop looks like this for(int i=5 ; i<=60;i++) .

Question 9:

  • Answer is (d) After each iteration through the loop.
  • for loop has format as follows for( initialization ; condition ; update){ …body to be excuted every iteration….}.
  • Here initialization is done only once for first iteration of the loop and then condtion is checked for beingTRUE or FALSE. If condtion is false then loop breaks.
  • But if condition is TRUE then the body of loop is executed first and then update is performed.

Question 10:

  • Answer is TRUE.
  • For security reasons access class should have limited access and visibility.
  • If class have public access then anyone can change the code and can cause serious damage to system.

Question 27:

  • Answer is (d) circle.
  • There is a class named circle which has properties like radius and color. These properties are nothing but variables storing radius and color of each circle.
  • Class circle also have methods two for calculating circumference and area.
  • Now we create objects of class circle with properties like radius color circumference and area.

Question 28:

  • Answer is (a) The main method is commonly used to instantiate objects and (d) The main method should be as simple as possible.
  • For option (a):: Generally program starts its execution with main method and to use properties of classes in project we need to create objects of those class so that we can call methods from class.
  • For option (d):: If all the calculations are done in main method then it gets complicated to debug the code if errors are present. Also the code is not easy to understand, So it is important that reapeatind code must implemented in form of small methods and then called in main.

Question 29::

  • Answer is (b) ArrayList<Boolean> arrList = new ArrayList<>();
  • Boolean here is wrapper class which is used to convert primitive boolean into object and object into primitive.
  • Primitive means variable of type boolean.
  • Rest statements give error.

Question 30::

  • Answer is (c) String.
  • String is used to declare variables of type string.
  • Boolean, Integer, Byte these are wrapper class used to declare objects of type they represent.

Question 5::

  • Answer is (d) The counter which gets incremented determines the number of loop iterations is within the parentheses of a while loop.
  • The while() loop is used when we don’t know how many times we need to run the code.
  • In this situation while loop is used. At every start of the loop the condition is checked to run the loop or to break.
  • Condition is checked in parentheses of the while loop. It is boolean exprsession which return true or false.
  • Suppose the condition is whether i==0 or no, if i is equal to zero then loop is executed and also i is updated.
  • Loop exits when condition returns false.
Still stressed from student homework?
Get quality assistance from academic writers!