#Some MCQ's on code
1 messages ยท Page 1 of 1 (latest)
<@&987246883653156906> please have a look, thanks.
While you are waiting for getting help, here are some tips to improve your experience:
If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.
Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.
Here is an AI assisted attempt to answer your question ๐ค. Maybe it helps! In any case, a human is on the way ๐. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.
Sure, I can help you review some multiple-choice questions on Java code. Please provide the questions and their options.
do u have a specific question?
(note that we wont just tell u the correct solutions)
yeah what is Extensible in these context I didn't even understand it
those are previous year oracle hiring Questions and I am practicing them
can I know y?
ur supposed to learn. and just telling u "check this this and that" wont help u learn
especially in case its an exam u might be taking right now, in which case it would be cheating
(which its not in this case)
extensible in this case means what they said right next
"to be able to handle more types"
right now, the code can handle truck, car and bike
U can answer me a day later too but just le me get the understanding of Extensible
what if u add 4 more types tomorrow
u would have to come back to this method and add like 10 more lines
they are asking how u can improve the code to make that easier
or more natural
making vehicle as a parent class for various child class?
then it means a switch case?
well. id say it isnt super clear what they want from u
i have no Idea as I got these pics over telegram to help prepare for placement drive
what would u say are ways to improve this code?
I too got confused by the context
with respect to the three answers
im not really confused, but i think that 2 answers could be reasonable to pick
although im fairly confident about which they want u to pick
would u know how to write the code for all 3 answers? or is any of the answer confusing u?
I got 1 and 2 as a answer but Its not mentioned multiple options right !
code is simpler these Structural or design Questions make me mad as a new programmer
this is more confusing for me
well, u gotta pick one of them. and i think its fairly clear which to pick, once u actually write down the code for the 3 answers
switch case is the simplest ?
but is it also helping in making the code natural for extension?
what if u have 200 types
then this method will end up having 200 switch cases
really long method
no then polymorphism
i agree that a switch case is more readable than the ifs here. but is it really what they want, design-wise?
how would the polymorphic solution look like? would there be if-cases still?
where and how would the emission be computed then?
still in vehicle?
design is not tough to us yet I mean not as a beginner
design is the single thing that separates a junior from the senior
im not really sure u got the 2nd answer yet, cause u constantly evade my questions on it
so the best among those 3 are switch or polymorphism
i wont tell u which is best before im sure u understood it
please dont evade
how and in which class would the carbon emission be computed in the second (polymorphic) solution?
ok sir can u answer me later so that count cheating as u imagine
it wont help u to remember by heart which answer to check
when u take the exam, it will be different questions
u have to actually understand it
for each class they must compute it differently
how would that look like? could u write a quick snippet?
class Vehicle {
// ...
}
class Car extends Vehicle {
// ...
}
let me put it right switch wont work u have to manually do it
but in case of polymorphism can do it
how would polymorphism make it work better
please fill in the blanks of the above snippets
class Vehicle {
// ...
}
class Car extends Vehicle {
carbonEmission(){
return self.engineSize * self.tire * 2.5;
}
}
Detected code, here are some useful tools:
class Vehicle {
// ...
}
class Car extends Vehicle {
carbonEmission() {
return self.engineSize * self.tire * 2.5;
}
}
and what would u have in Vehicle?
just make a over ride
wait a sec
class Vehicle {
carbonEmission(){
return self.engineSize * self.tire;
}
}
class Car extends Vehicle {
@override
carbonEmission(){
return self.engineSize * self.tire * 2.5;
}
}
Detected code, here are some useful tools:
class Vehicle {
carbonEmission() {
return self.engineSize * self.tire;
}
}
class Car extends Vehicle {
@override
carbonEmission() {
return self.engineSize * self.tire * 2.5;
}
}
okay, alright
some minor issues in that snippet, but doesnt matter
seems u got the overall idea
๐
so this approach will create a way to naturally extend the logic with more types
this was very cool learning with u sir!
instead of having a centralized place that controls them all
ur welcome
now to ur second question
what about this one
this is literally out of my mind
lets talk about each option and see if it helps
it should be very clear then
the problem they are asking u to solve is to make the code more readable and easier to understand
how would the first option look like?
it will make a looot of nesting then
similarly 2ed option is a business logic to change
if (user.experience > 6) {
if (user.earnings >= 5000) {
...
}
}
yes, nesting
does that help?
is it more readable?
it is but its more nested isn't it?
what if there are 20 conditions
ull get hadouken
not readable
so that option is from the table, not good
second answer, as u said, changes the logic
so thats from the table as well
what about the last answer, else statement
3ed one is the same no change the readability will decrease in function
4th adding a else at the end will have no change as else doesn't carry any condition
well, the question would be rather what to put into its body
but yes
disagreed ๐
if (isRisingTalent(user)) {
sendInvitation(user);
user.risingTalent = true;
}
this is MUCH more readable
u can read and understand this logic in a second
while for the old code, u need to read for 15 seconds
yes
and then u put the conditions into a helper
private static boolean isRisingTalent(User user) {
return user.experience > 6
&& user.earnings >= 5000
&& user.contracts > 60
&& user.warnings == 0;
}
which, in itself, is also easy to understand
since its now decoupled from the logic
and has a fitting name
dam
its super cool to learn those
thanks a loot sir
shall i close the Questions?
if u dont have any questions to that anymore right now, sure ๐
thanks sir โค๏ธ