How to Test Java programming skills Online Quiz 04

Explanations of answers for Quiz 04

Lesson Details:
January 14, 2020

This course is brought to you by Appy Pie as part of our Academy series all right so here is our next very simple question the question is the break statement is used to exit from the options are if a statement loop and a program so these are the three options if statement loop a program and the correct option is loop let us check the answer and good job alright so this was a pretty simple question indeed hey then here is a yet another question and we need to determine the output for the same here first of all we have the main method and inside the main method we have a for loop now the for loop is different from usual because the initialization part is before the for loop and the increment of the variable part is inside the for loop now for example if we move all of these parts to its proper location then let me show you real quick alright so this looks good to me so if I keep each and everything at the usual location then the output would be true false false false false well this seems to be good enough but when we differ the location of these elements a little then the output will also change a little so now the output is false false false false false so there is a change in the first iteration and the output differed a little now the reason is pretty simple when we keep this incremental part inside the for loop then the incremental part would be performed just after execution of all the statements which are inside the for loop in other case if we keep this incremental part inside the for loop in that case there won't be any special treatment for this incremental part and as a result the incremental part will increase the value of I by one first and then the next statement will get executed and as a result there would be some change in the first iteration in this example and as a result the correct option is false for five times so this is I suppose good explanation for this example hope you guys enjoyed the explanation thanks for watching hey then here is a yet another program and we need to determine the output for the same now in this program we have the main method and inside the main method we are making use of the for loop while loop and dowhile loop now this program is quite interesting when compared to other programs in here we have first of all indentity the value of int s5 and definitely the name of the indentity is AI now whenever there is a loop inside loop always make sure to check the inner loop first that will give you better understanding so the innermost loop is the do-while loop so let us check the do-while loop first now if we check the do-while loop first of all the condition of do-while loop is false this means that this loop will only get executed once in the lifetime moving forward inside this loop we have this a statement which will print 1 on the console and we are making use of print and not trained allen so this will print 1 and the cursor will not reach the new line all right moving forward we have a condition now whether this condition is true or false is doesn't matter because we are not printing anything inside the body of the F condition all right so this seems to be good enough so this loop is as good as only this particular statement inside the while loop and this loop will simply display one that too once in its lifetime all right so moving forward when we check out the while loop the while loop is an infinite while loop because the condition is true but there is a catch in here the while loop will definitely execute this 2-mile loop inside the iteration but then it will reach the break statement this means that the iteration will break and it will just execute once so this while we'll also have no effect and this particular what we say a statement would be printed once in the lifetime of a while loop and dowhile loop so this is as good as having this single statement inside the for-loop itself and then finally we have the for loop and the for loop is the usual for loop we will iterate four four times in this program so let me execute this program and the output is pretty simple 1 1 1 1 all right so let me just get rid of this while loop and dowhile loop in order to show you the actual output and let me execute this and this will give you the same old output so this is how this question played with you and the correct option is definitely 1 1 1 1 so thanks for watching and I hope you enjoyed the lecture hey then here is a jetan of the program and we need to determine the output for the same here we have a class by the name of quiz and then we also have the main method inside the main method we firstly have a int array the name of the array is a and there are in total four elements in there and these elements are 0 2 0 7 0 1 0 and 1 1 so these are 4 elements which is good enough now moving forward we are making use of for each loop now this for loop is a different loop where we can iterate through a collection of entity in this case this is an array and we can iterate through each and every location one by one alright so in here we would be iterating through this array and we would be displaying the value one by one no friends where is the catch in this program this program seems to be very simple right so the catch is in this int array in here the second element has a 0 as a prefix no here is the catch now for example if I execute this program then in here the output is 2 then 56 then 10 then 11 now why there is a 56 because there Cemal equivalent of octal 70 as 56 so definitely the output is 256 10 and 11 so this was the explanation and this program is simple enough thanks for watching and I will catch you in the next one hey then here is as yet another program and we need to determine the output for the same alright so in here we have the main method inside the main method we have a string array right and what are the contents of this a string array X Y and Zed alright now moving forward we have a for each loop now understand properly whenever we make use of a for each loop in here there as a temporary value which would be used in our for each loop the name of the temporary value in our case is value now in this temporary value we are simply reassigning the value K now this will have no effect on the original copy of your string array and as a result when we will try to display the output from the system dot out dot print statement then definitely now it would be XY and said let me show you on eclipse and let me execute this program and here is the output so this was a small little trick which was played with you in order to give you a confusing small little program and the correct option is X Y Z alright so this was a quick explanation hope you guys enjoyed it thanks for watching alright moving to the next question we can change the size of array ones created definitely a false statement good job hey then here is a yet another program and in here we have a class by the name of quiz and then we have the main method now if we check the main method in here first of all we have a int array by name of a are R and there are a couple of elements inside this array now if we like check the elements of the array the elements are 0 1 2 3 4 5 and 0 again and if we count the number of elements which are there in here then we will find there are one two three four five six and seven in total elements then moving forward we are simply initializing a yet another int entity the name of the nth variable as X and we are initializing the value of X as 0 as a RR of 0 will give us 0 alright so this is good enough now there is a catch and the catch is inside this F block now we need not to worry about the iteration at all why because this if block will always give you a false result how a RR of I can have values 0 1 2 3 4 5 or 0 right and the value of x is 0 now 0 is less than 0 condition is false now if a RR is any number greater than 0 then definitely the output would be false for example a RR of I as for example will contain the value as 3 3 is less than 0 well the condition is false again this is the reason why this iteration will have no effect at all and the value of X will remain as 0 and finally the output would be simply 0 alright so here is a yet another question and the question is pretty simple determine the output of the program given below and that this program is also very simple and definitely there would be an exception after execution of this program why let us check in here there is a class by the name of quiz right and there is a main method and inside the main method we have array the size of the array is 9 but in the next statement where we are trying to print element of the array we are simply trying to access the nth element of the array as any starts from 0 so as a result we are trying to access the 10th element of the array and definitely there would be one exception and the exception would be array index out of bound so let us check this option third option is the correct option just check the answer for the same and good job let's move forward and check the next question alright so this question says which of the statement can get the length of the array that follows in here the statement as end array customers equals to new and 55 now in here there is this int array customers and the size of this array is 55 which is good and moving forward in here there are a couple of options and definitely the second option is the correct option because accessing the length of the array here is the way which is in front of us we need to place the name of the array dot length length is the property of the array all right so let us check our answer good job once again moving forward finally the last question of this quiz the question is what would be the output of the program segment and the program segment is pretty simple in here we have an array int array and there are in total seven elements in this array and then finally we are trying to access the array with index one and the array with index three now in here what would be the value at index ID one definitely two value would be one because the index of an array starts with zero likewise the value at the index ID 3 would be three in fact if we add these two values one and three definitely the output would be four so the third option is the correct option let me check this and opie good job once again.

loader
Course content