How to Test Java programming skills Online Quiz 02

Explanations of answers for Quiz 02

Lesson Details:
January 14, 2020

This course is brought to you by Appy Pie as part of our Academy series alright so the next question is a private variable can be accessed by and there are a couple of options let me read the options for you only the class element to where the variable is defined the next option is any class then the next option has any class within the same package and the last option isn't enough about definitely the first option is the correct option why because overall idea of private modifier is to give you privacy right and privacy is something which will restrict the outside world to access Thank D right so only the class elements where the variable is defined can access the private variable let me check the answer and gotcha hey there in this program we need to quickly identify the output of the program now this program is pretty simple to understand where we have a class by the name of quiz then we also have a main method inside the main method we have couple of statements and you also have a yet another method now friends in each and every quiz question there would be a catch somewhere and in this program the catches at line number 3 where we have initialized a int variable with 10 and we have also marked the variable as final then in the next line we try to the assign a yet another value to the final variable and definitely this will give us a compile time error and if we quickly try to check the same code on our Eclipse IDE definitely that would be a compile time error so the final result of this program would be a compile time error and I suppose the third option is compile time error for this question so the compile time error is the correct option or the compilation error in other words all right so this is all about today's lecture thanks for watching hey there today we will check a yet another program we'll try to determine what is the output of the program so in here again we have a class by the name of quiz and firstly we have a static variable and the name of the variable as X and we have initialized this static variable using the value 99 moving forward we also have a main method and from the main method we are simply calling a method by the name of call and we have simply passed the value 10 to the call method alright if we check the call method the call method is first of all a static method so this method can easily access a static entity so that's one thing moving forward this receives integrity and we are simply passing the value 10 to this end entity now moving forward we are simply trying to assign the value 10 to variable X and note that we are making use of this operator now as usual there would be a catch in this question as well in the catch in this program is at line number 7 in here we are making use of this operator and whenever we talk about static variable this is not the way to access the static variable all right so let's proceed further and let's quickly check Eclipse in order to check what will be the output of this program and definitely the output of the program would be a compile time error now if we try to understand where is the issue first of all this operator is used to access the instance variable which are definitely the global variable alright so in here we need not to make use of this so let me get rid of this and then if I try to reiax acute this then definitely the output would be 10 but if we would like to access this aesthetic entity whose value is 99 then that is pretty much possible we simply need to make use of the name of the class so the way is pretty simple we will type in quiz dot X and then you can see the output would be changed here we go so the value of x equals 99 is the because we are simply what we say trying to access the static variable instead of local variable so this is all about today's lecture and I hope the explanation is good enough and definitely whenever you would like to access a static entity and the static entity is like global variable then definitely make use of a class name dot variable name alright so this was it I suppose the explanation was good enough and I will get you in the next one hey there so here is a yet another question and again we need to determine the output first of all we have a class by the name of quiz and inside this class first of all we have a static block and inside this a static block we have a variable with a name as a the value assigned to a is 5 and inside this is a static block we are simply displaying the value of this variable a moving forward we also have the main method and inside this main method we are again trying to display the value of a now this program is a little different than usual because we are simply like printing the value of a in our static block and in our a method as well and the output would be also strange and different from usual so let us quickly check our Eclipse IDE and here is our program now if I execute this program then we will get a output and we will also get a compile time error strange yes it is a little strange let me show you here we go so first of all we are getting the output as 5 and then there is a compile time error and the issue is pretty simple we are simply trying to access a element which is not available to the main method understand this that whenever there is a static block then the elements would be only available to that static block only and this is the reason why we are able to get the output as 5 the second rule is that if there is a static method then it would be executed before your main method so definitely the static block would be executed first and then your main method would be get executed as a result first of all we will get the output as five but as soon as Java will try to execute main method that would be a trash and that would be a compile-time error all right so this is today's explanation I hope the explanation was easy enough and I will catch you in the next one hey there so here is yet another program and we need to determine the output and this program is comparatively simple to understand let's check this out all right so any here first of all we have a class by the name of quiz and inside the main method first of all we have initialized variable the name of the variable has value and we have initialized value as 99 then we have called a method by the name of change value and we have passed the value variable to the method and finally we are displaying the value of value variable now if we check this particular change value method this is a static method now this change value method receives our integrity and inside this change value method we are simply assigning 10 to this variable value now where is the catch in this program now the catch in this program is pretty simple we will simply pass this variable as call by value and not call by reference so to this method change value we will have a local instance of this variable value and we are simply like modifying the value of this value variable which is okay but as soon as this method will retire the variable value which is local to this change value method will also die in fact the effect of this call to change value method would be nothing as we are simply passing the value to this change value method all right so finally we will display the output and definitely the output would be 99 let me also show you this program directly on Eclipse and let me execute this and here is the output 99 so this is all about today's lecture hope you guys understood the concept and I will catch you in the next video hey there here is yet another program and we need to determine the output now this program is again very simple and here we again have a class by the name of quiz and we also have a constructor this constructor receives a indentity and we are simply like receiving the value of score and then we are assigning score to variable score alright moving forward we also have a main method and another main method we are simply printing a message a score of the quiz and then we would be displaying the value of the variable score so this seems to be good enough and if we execute this particular program what should be the output the output should be zero even though we are setting the value as 10 let me reiax acute this and show you as usual there is a catch in this program as well and the catch is at line number 5 where we are assigning a score to score now this is score which is received by this quiz constructor this is a local entity and we are simply passing the value of this local variable score but if we would like to initialize the global variable score then definitely we need to make use of this operator and the reason is pretty simple we need to explicitly tell Java thereto which version of score needs to be initialized and how we will do it we will simply type in this dot score and now we should get a proper output a score of the quiz as 10 so this is pretty simple and the answer for the question is 0 and choose the correct option which are provided for this particular question and the answer is 0 again thanks for watching hey there here is the program and we need to determine the output for this program and if we try to execute this program there is a crash and the reason of crash is pretty simple if we check the main method in here we have the class quiz and we also have the main method inside this main method we are simply trying to print the value of a now the a entity is definitely a private entity inside our class a so that's a point and we are trying to create a anonymous object of Class B now if we check the Class B Class B extends a which is good enough but whenever we extend a class only the public or protected properties would be available to the subclass as a result the object of B doesn't contain the property a and we are trying to access a private property of Class A and hence that is a crash so very simple very straightforward the basic rule which we need to remember is that if we are extending a class only the protected and public entities would be available to us all right so this is all about today's lecture hope you guys enjoyed it and I will catch you in the next one now the second question is a package is a collection of definitely a package is a collection of classes and interfaces this is a theory type of question and always remember the answer of it a packages a collection of classes and interfaces let me check the answer good job let's proceed further the next question what would be the value stored in variable X now let me check here is the part where we are restoring the value of X the first part where we are initializing the value of x equal to zero it won't have an effect because we are reinitializing the value right so the value of x would be 3 3 would be stored on Z and the value of Z would be stored into our variable Y and then finally value of 5 would be stored on X again and what would be the value of x y&z; obviously all of these three variable will start storing the value 3 so let me select this option oops something went wrong I suppose I am NOT able to select this option all right so that worked let me check the answer wonderful and then let's move forward then finally the last question of the squares and object is created with new keyword well good enough depends on the code at runtime and compile-time all of our and the correct answers at front time let me check the answer good job.

loader
Course content