Transcript for:
Java Building Blocks - Summary & Exam Essentials

foreign so in this particular lecture this is actually the last lecture of the first chapter which is Java building blocks so this this lecture will be just a summary of this section uh some tips and also we are going to see some exam questions which may come from this section so this is also a very important lecture so make sure you are attentive and yeah I hope you like this lecture so some of the summary and exam Essentials that I want to point it out right now the first point is to be able to write code using the main method so a main method is usually written as a public static void main string arguments so any arguments are referenced starting with args of zero right because it's an array so accessing an argument that wasn't passed in will cause the code to throw an exception so I think that is pretty much of clear next point is understand the effect of using packages and imports okay so packages contain Java classes right so classes can be imported by class name or wildcard wild cards do not look at subdirectories so in the event of a conflict class name Imports take precedence we have discussed this in detail right um be able to recognize the Constructor a Constructor has the same name as a class name be aware of the tricky questions which may surround in this particular topic be able to identify legal and illegal declarations and initializations so multiple variables can be declared and initialized in the same statement when they share a type okay local variables require an explicit initialization others use the default value for that type that is the class variables and the instance variables okay be able to determine where variables go in and out of the scope we have already discussed this in great depth okay regarding the scope of local variables instance variables and class variables be able to recognize misplay statements in a class so package and import statements are optional okay if present both go before the class declaration in that order okay fields and methods are also optional and are allowed in any order within the class declaration okay and the last point but not the least is to you know um know how to identify when object is eligible for garbage collection okay so that was from our last lecture so obviously draw a diagram to keep track of references and objects as you trace the code so when no Arrow points to a particular box or object in this case it is eligible for garbage collection okay so I think it is pretty much of what we have covered in this particular section and yeah that's it now we are going to see some questions we are going to see 20 to 25 questions regarding from this section and um so what you can do is after reading a question you can pause the video and can answer it on your own and then I'm going to explain and I'm going to say the answer to that particular question okay make sense all right let's go so the first question which of the following are valid Java identifiers choose all that apply okay so you can pause the video and answer the question so let's see the answer the answers are a b and e option A is valid because you can use the dollar sign in identifiers okay option b is valid because you can use an underscore in identifiers option C is not valid identifier because true is a Java reserved keyword okay option D is not valid because the dot is not allowed in identifiers make sense option e um it is valid right because Java is case sensitive so public has a capital P instead of a small P so small P public is actually a reserved keyword so yeah and also pay attention to these multiple uh choice questions because you may miss like okay you see that a is a valid identifier you skip through all the options but don't do that because go for uh this multiple choice type questions wherever possible okay let's move forward so what is the output of this following program okay now let's see the answer to this question the answer is d so Boolean Fields initialize the false and references initializes to null okay so empty is false and brand is null so brand equals to null is output okay so I hope this makes sense we have we have seen this in default initialization of instance variables okay the third question which of the following are two true so we have short numpets equals to five in Num grains equals to 5.6 string name equals to Scruffy and then we find trying to find the length now the answer to this question is b d and e d d and e so option A on the particular line number four compiles because shot is an integral type right integrator which is an integer kind of type option b line number five okay generates a compiler error because int is an integral type uh but 5.6 that we see is a floating type okay option C Line 6 compiles because it is assigned to a string option D and D which are lines seven and eight do not compile because short and int are Primitives okay so Primitives do not allow methods to be called on them and option F line nine compiles because length is defined on string so length is a particular method okay and it is available in the string class you are going to cover string in details in a later chapter so don't worry about it let's go to the next question so given the following class which of the following is true so we have a class snake Okay and there is a method public void shed um and then we are you trying to use the particular um local variable time okay let's see let's see what is the answer so the answer is a and b now the reason being sorry the reason being adding the variable at line two right adding the variable at line to makes result and instance variable okay now since instance variables are in scope for the entire life of the object option A is correct okay option b is correct because adding the variable at line 4 so when we add the variable at line number four makes result a local variable with a scope of the whole method okay make sense adding the variable at line number six makes result a local variable with a scope of lines 6 and 7. since it is out of the scope in line number eight okay the print element does not compile and option C is correct sorry option C is incorrect my bad now adding the variable at line number nine makes result a local variable with a scope of lines 9 and 10 since line 8 is before the Declaration right so it doesn't compile in that sense and option D is also incorrect finally option e is incorrect because the code can be made to compile okay it's it's not that the the code is never be able to compile we can make the code compiled by using options A and B okay all right let's go to the next question given the following classes which of the following can independently insert uh sorry independently replace insert uh Imports here to make the code compile okay so let's see the answers right so answers are C and D I hope you guys have answered the right options okay so the answers are C and D option C is correct because it Imports jelly by class name right and option D is correct because it Imports all the classes in the jellies package so both will work no issues in that so in the jellies package includes jelly right option A is incorrect because it only Imports classes in the aquarium package so tank in this case and not those in the lower level packages because remember when you are importing from packages it only Imports the classes from that particular package okay option b is incorrect because you cannot use wild cards any place other than the end of an import statement we have discussed this in detail right option e is incorrect because you cannot import parts of a class with a regular import statement it's not possible option f is incorrect because option C and D do make the code compile so it's possible all right now given the following classes what is the maximum number of imports that can be removed and have the com code still compiled okay let's see the answer is e which is 4. now the first two Imports can be removed because java.lang is automatically imported right so it is automatically imported the second to Imports can be removed because tank and water are in the same package okay make sense making the correct answer e so if tank and water were in different packages one of these two Imports could be removed in that case the answer would be option D okay right um I think that that is it for this particular question let's move on to the next question so it's a kind of a big question all right but make sure you pause the video and look at it uh in a careful manner so let's see the answers the answers are a b and the answer will also be C I don't know why I left C out but C is also an answer so the answer will be a b and c so option A is correct because it Imports all the classes in the aquarium package okay including aquarium dot water okay options B and C are correct because they import Water by class name okay since importing by class name right takes precedence over wild cards these compile without any issues option D is incorrect because Java doesn't know which of the two wild card water classes to use we have already seen this Clash IFC I have explained it in an example right so option e is incorrect because you cannot specify the same class name in two Imports right so that is that so the answers are a b and c all right let's move on so given the following class which of the following calls print out blue J so so pause the video and yeah Mark your answers so let's see the answers answer is option b option b is correct right because arrays start counting from zero we've seen this right we're going to dig deeper into arrays in our later chapters but for now it is I've already told that arrays start from index number zero right and strings with spaces must be in quotes right option A is incorrect because it outputs blue C is incorrect because it outputs J option D is incorrect because it outputs Sparrow okay option E and F are incorrect because the output error could not find or load main class but display dot class so that is that is not possible so option b is only the correct solution okay so I hope that is it for this question let's go to the next question which of the following legally fill in the blank so you can run the main method from the command line okay so let's see the answers I can pause it and you can try it on your own let's see the answer so the answers are a c d and e okay let's see how it is happening so option is correct because it is the traditional main method signature okay and variables May begin with underscores there is no issues with that options C and D are correct because and because the array operator may appear after the variable name okay option e is correct because varags are allowed in place of an array now this can be a tricky question because I have not talked about vararks in the section but varavs are kind of uh kind of an operator we which uses Three Dots and using varas you can actually input multiple elements they are basically used in used as parameters in a method or somewhere else it kind of acts as a replacement for arrays as parameters we are going to discuss this in detail don't worry about that but you can use varags instead of an array as a parameter okay there are rules regarding varaks we are going to discuss that later but for now just understand that if you see this kind of question then varags are also an option okay so option b is incorrect because variables are not allowed to begin with a digit so that is one of the rules option f is incorrect because the argument must be an array or warx um option f is a perfectly good method however it is not one that can be run from the command line because it has the wrong parameter type okay so yeah that's the that's the thing okay so let's move on to the next question okay let's go to question number 10. um which of the following are legal entry point methods that can be run from the command okay let's see the answer answer is e option e is the canonical main method signature right so it's kind of a very easy question but still you need to memorize it so you need to memorize this particular um you know structure option A is incorrect because the main method must be public option b and F are incorrect because the main method must have a void return type okay option C is incorrect because main method must be static okay option D is incorrect because the main method must be named Main so yeah that's that's about it next question which of the following us are true so there are a b c d e f g so I think that it's seven options choose the right answer okay so the answers are C and D so option C is correct because all non primitive values default to null uh we have already discussed right so if it's very easy to remember because if you have any kind of let's say a reference type and you haven't initialized it then it will refer to null option D is correct because float and double primitive default to 0.0 okay options B and D are incorrect because int Primitives default to zero okay make sense let's move on so what which of the following are true and we have a lot of options let's see what is the answer to this question answer is G none of the above why option G is correct because local variables do not get assigned default values right only the instance variables and the class variables are assigned default values the code fails to compile if a local variable is not explicitly initialized okay so if this question were about instance variables then option D and F would be correct a Boolean primitive defaults to false and a float primitive defaults to 0.0 okay next question which of the following are true kind of the same types of questions but the options are different yes so the answer in this case is um A and D okay options A and D are correct because Boolean primitive default to false and in primitive default to zero okay next question given the following class in this particular particular package ah so basically which of the following replaces insert code here if we compile from slash my slash directory can be a tricky question but if you pay attention to this thing then it is pretty easy let's see the answers are D okay answer is d y d basically the package name represents any folders underneath the current path okay which is named a in this case option b is incorrect because package names are case sensitive just like variable names and other identifiers okay that makes sense okay let's move on question number 15 which of the following lines of code compile answers are A and E underscores are allowed as long as they are directly between two other digits we already know that right this means options A and D are correct options B and C are incorrect because the underscore is adjacent to a decimal point which is not one of the rules right option D is incorrect because the underscore is the last character okay 16 given the following class which of the following lines of code can replace insert code here to make the code compile okay let's see the answers to this question answers are b c and d so basically 0 be right we have discussed this is the prefix for a binary value right okay and it is correct 0x is the prefix for a hexadecimal value okay this value can be assigned to many primitive types including int and Double Making options C and D correct okay option A is incorrect because 9 L is a long value so long amount equals to 9 L would be allowed okay option e is incorrect because the underscore is immediately before the decimal okay option f is incorrect because the underscore is the very last character okay make sense let's move on which of the following are true so we have a public class bunny we have a public static void mainstream arcs and we are creating an object of the bunny class so let's see what are the options which are correct A and E A and E so bunny is a class right so which can be seen from the Declaration public class bunny right you can see that there and one is a reference to the object main is a method okay so that makes sense question 18 which represents the order in which the following statements can be assembled into a program that will compile successfully so we have to just see the order which will make it compile right let's see the orders so C D and E are you know effective Solutions why is that because package and import are both optional right if both are present the order must be packaged then import then class right option A is incorrect because class is before package and import which doesn't make any sense option b is incorrect because import is before package okay option f is incorrect because class is before package option G is incorrect because class is before import so making the option C D and D the correct Solutions okay so yeah another big question so basically you know that the the the exam will kind of give you a lot of bigger questions and sometimes you know the bigger questions have the most easiest Solutions as we move on move through this particular course right you will be encountering such kind of questions and as you practice more and more it will become easier for you okay so let's see the answers to this particular question I hope you guys paused and answered your own questions let's see the question uh answer B and D are the answers why the rabbit object from line 3 has two references to it one and three and this is the garbage collection question so make sure you um draw the diagram right that we have discussed how to draw it so the rabbit object from line three has two references to it one and three we already see that the references are nulled out on six and eight line number six and eight if you see option b is correct because this makes the object eligible for garbage collection after line 8. line 7 sets the reference 4 to the now null one okay if you see line number seven over here what it does basically it sets the reference 4 to the null one which is already null this one was null right which means it has no effect on garbage collection so you are just referencing a null value in this particular scenario the rabbit object from line number four that you see over here that line number four object only has a single reference to it which is 2. right option D is correct because this single reference becomes null on line number nine in this particular scenario over here the rabbit object declared on line 10 becomes eligible for garbage collection at the end of the method on line number 12. okay because that's where the scope ends that's where the method ends right so calling system.gc has no effect on the eligibility of garbage collection because I've already told you that system.gc will not instruct Java jacket to garbage collector Java will ignore that request okay so it makes sense in this case why B and D are the actual Solutions okay if you get confused please draw the diagram you will be very very much you know um confident about the answers question number 10 sorry 20. so what is true about the following code so here is the code pause the video and answer the question let's see the answers answers are B and D okay so calling system.gc right I've already told you suggest that Java might wish to run the garbage collector but it doesn't actually may or may run it depends on Java Java is free to ignore that request making option e correct finalize runs if an object attempts to be garbage collected making option b correct okay all right question number 21 so let's see the answer what does the following output what is the output of this particular question answer is a which is 0 while the code on line 3 does compile right it is not a Constructor because it has a return type all right it is a method that happens to have the same name as the class when the code runs the default Constructor is called and count has a default value 0 which is for the int okay question number 22 let's see the answers B and E so this is related to operator overloading right and um as far as you know Java is considered C C plus plus is kind of a language has operator overloading and pointers okay Java made a point of not having either it doesn't have operator overloading or pointers okay now Java does have references to objects okay but these are pointing to an object that can move around in memory so option b is correct because Java is platform independent option e is correct because Java is object oriented while it does support some parts of functional programming these occur within a class okay so Java is not a completely functional programming language we are going to see the functional programming features of java in the later sections of this course when we are going to learn about lambdas and everything but it is an object oriented language okay so I think this is the last question so which of the following are true answers are C and D uh so basically Java puts a source code in dot Java files and byte code in dot class files okay it does not use a DOT byte code file when running a Java program right you pass just the name of the class without the dot class extension so C and D are correct all right guys so that's it about this section so we have reached the final lecture of um Java building blocks which is basically the first chapter and I think it is the most longest chapter okay we are going to start with the next chapter which is operators and statements we are going to see a lot of things about operators and statements we are going to see types of operators uh we are going to see Advanced flow control and everything we are going to see everything from start to finish so make sure you subscribe to this Channel and make sure you will receive all the notifications regarding our future lectures alright guys that's it from my side uh see you in the next lecture till then have a nice day bye