How to Test Java programming skills Online Quiz 01

Explanations of answers for Quiz 01

Lesson Details:
January 14, 2020

This course is brought to you by Appy Pie as part of our Academy series hey there here are the explanation for the first three questions which are there in the quiz the first question is which one is a valid declaration of a boolean definitely a boolean variable can contain either true or false so definitely the third option is the correct option and true and false our keyword so we need not to make use of any single quotes in here alright so this seems to be good enough let us check the answer and good job all right so let's move forward and the second question is which is the encoding scheme for characters used in Java definitely unicode let us check the answer again a good job let's move forward and size of int datatype in Java definitely 32-bit alright so good job so these were the first three question in our quiz 1 thanks for watching and meet me in the next video hey then here is the question which we will discuss in this video and this is a pretty simple question where we need to determine the output of the code which is in front of us now in this small little program we have this class by the name of quiz and we also have two instance variable by the name of I and J and inside the main method we are simply calling in the method sub and inside the sub method we are simply subtracting the instance variable I from J and the value would be stored inside this variable key and finally we are printing out key now if we check this program the possible output could be for example 1 because we are subtracting 11 from 10 right then 0 won't be the output and there are two more options which are compilation error with an error at line number 5 and compilation fail with an error at line number 8 right now let us quickly move to eclipse and check out this program in real and here as Eclipse and I have already like created this program for you let me execute this and that would be definitely a error and the first error is on line number eight let me expand this and show you so the first error is line number eight and if I highlight that error then here is the error which is there now why there is a error now there is a very basic issue with this program first of all this method is a static method now there is a generic rule we can only access aesthetic elements inside a static method and in here we are trying to access instance variable now how to fix this issue pretty simple we can get rid of just a static keyword and then try to for example execute this program and then again there would be a issue at line number five because we need to call this sub method by using a object so let's do that so I will simply type in new squares dotsub and then our program should work let me execute this and here is the output which is perfect right the second workaround is let me undo the changes we need to mark these instance variable as a static then again this program will work here we go now why there is a error when we are trying to access the instance variable the answer is pretty simple so let's get started and we've already do the same all right so why there is a error when we are trying to access an instance variable inside a static method now the reason is pretty simple whenever we are like creating an instance variable the instance variable are based on instances or in other words the instance variable are based on the object we have created so the instance variable location are and based on how many objects are there different memory location would be used to store the information whereas static elements would be stored only once in the memory and the memory location of aesthetic elements would be in a different heap of memory if there is any confusion in your mind do research on static memory location and instance variable memory location and that will give you a better understanding now moving forward as quickly check the options for these questions and definitely the answer for this particular question is the fourth option where the compilation failed with an error at line number 8 because we will encounter with the error in this 8 line all right so this is all about today's lecture hope you guys enjoyed it thanks for watching alright so here is the next question and in this question we have a class by the name of quiz and inside the main method we have two instance variable the variables are I the value is 0 1 2 then we also have a variable G the value is 0 6 and then we are simply printing in the value of I and J now here are the possible output of the program which is provided to us first option as 1 2 6 now this option is not correct because we are making use of println and definitely the value of I and J would be displayed in separate lines then we have 12 + 6 this answer seems to be good enough but this is wrong why because whenever B prefix 0 to any end value that particular into value would be considered as an octal value and the octal equivalent of 0 1 2 as 10 so the octal equivalent of 12 verse 10 as a result the first line the value of I would be 10 whereas the octal equivalent of 6 is again 6 so in the next line 6 would be displayed so the third option is the correct option hey there in this video we will check out this small little program and we will try to check what would be the output for the program all right so in this program we have a class by the name of quiz now in this class the first statement is declaration of a variable a and we have assigned the value 1 to this variable no friends note that this a variable is a global variable all right moving forward we also have a method by the name of method which receives int entity and the name of the variable as a parameter list is again a so inside this method we will make use of a local variable so there is a catch in there and then definitely we have performed some kind of operation on the variable a and finally we have displayed the value of game which is good enough moving forward we have the main method and inside the main method we are simply calling in the method method and we have sent the value as 2 to the variable a which is inside there in the method so little tricky to understand and no options which are given to us as 1 2 3 & 4 and first of all let us understand what will be the correct output we have passed the value 2 to this method method so the value of this a local variable is to initially and then we have performed the operation the operation as a plus equals 1 this means that we have added 1 to the current value of a so the current value of a would be 3 and definitely before printing the value to the console we have incremented the value of a once again as a result the output 4 before now there is nothing to do with the global variable in this program so this is 8 and if we quickly try to check what is the actual output by executing the code even that is with us let me execute this and here is the output in front of us the output is pretty simple for so this is all about today's lecture thanks for watching all right so the next question is what's the default value of boolean variable definitely the answer is false which is already highlighted and let us check the answer good job so this was a quick explanation hey there so here is the program which we will discuss today and the question is again based on a program and we need to determine the output of the program now this program is pretty simple where we have a class and inside the class we have the main method and there are a couple of operations and finally we are displaying the value of the variable I now in this program there is a catch and the catch is on line number 3 where we are dividing 4 divided by 8 now 4 is an INT entity and 8 is an INT entity so whenever there is an operation in between in entities then the output would be again a end so whenever we would be like divide 4 divided by 8 the output would be 0.5 but the 0.5 value would be converted into a indentity and the end value of 0.5 as 0 now as a result we will be simply multiplying 0 with 10 and then we would be storing 0 in our variable F the value inside the variable F would be zero point zero because again 0 would be converted into a float entity finally in the next line line number 4 we are simply converting 0.02 indentity and int equivalent of 0.0 as a 0 as it is no rocket science and finally the value 0 would be stored inside this variable I all right so enough of explanation and by now you can identify the correct option the correct option is a 0 which is the third option I hope you enjoyed the explanation hey there today we will discuss the following question and the question is when automatic type conversion take place in Java now this question is pretty important in fact and if we check the particular options which are provided then immediately the first part of the options are common first option is both element have compatible data type sighs of destination is shorter than the source type then the second option is both element have compatible data type and then the rest of the statement likewise in the third option as well the first phase is common both elements has compatible data type now what does this mean compatible data type let me show you this article from oracle.com real quick now if we check this article this article talks about the data types in Java and in total there are 8 primitive data type now if we check byte short int and long then these are like compatible data types for example the maximum positive value of byte entity can contain is 127 and 127 can be easily stored in a int variable as well so these are compatible with each other likewise float double are compatible with each other boolean is not compatible with any other data type likewise care as digit another different data type so the compatibility of data types are important so the first thing is pretty clear then there are a couple of statements in here and the third statement is the correct option for this question let me read the statement for you both elements have compatible data type and the size of destination the size of destination type is larger than source so this is important that the destination should be larger than the source then only the automatic type conversion will take place all right moving forward we will try to understand what does this mean let me open the article once again friends in here let us take example of byte and short if we check the range of byte the maximum positive value a byte can contain is only 127 but the maximum positive value in short variable can be stored and the maximum value is 3 2 7 6 7 now we can easily store a byte value into a short value because the range is way more larger than bite but if we for example try to store 3 to 767 in a byte variable then definitely the byte variable doesn't have the size to store such a big number so here the size matters now in order to understand this in a better manner here is a very sweet and simple example if we type and might be equals 127 then we type in short as equals B then this would be okay with Java because Java knows that short s can easily contain the maximum value of byte as well right but when it comes to short X as 30 and byte Y is equals to X and then in here there is an ambiguity Java has an objection in here because the maximum value of short cannot be contained in a byte entity so Java will not apply the automatic conversion all right so this was a short example related to automatic type conversion definitely the third option is the correct option I hope I have made myself clear hey there so today we will discuss a yet another question and this is a simple and easy to understand question indeed so the question is in Java byte short int and long types are and the options are signed and signed both of above none of above and the correct answer is signed let me show you these example whenever we type in for example int x equals 5 it means int x equals plus 5 so the plus is the default sign for these variables maybe int or made we float the default sign is plus moving forward we can also specify the default sign explicitly so it is perfectly alright to type an inside equals plus 2 which is easily acceptable moving forward if we would like to make use of minus sign even that is possible we simply need to specify the minus sign explicitly and we may happen into y equals minus 9 so this was all about the story of signed variables I hope you got the point and I will catch you in the next one .

loader
Course content