#Some MCQ's on code

1 messages ยท Page 1 of 1 (latest)

frail egret
errant stumpBOT
#

<@&987246883653156906> please have a look, thanks.

errant stumpBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

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.

crisp crown
#

(note that we wont just tell u the correct solutions)

frail egret
frail egret
crisp crown
#

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

frail egret
crisp crown
#

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

frail egret
frail egret
crisp crown
#

well. id say it isnt super clear what they want from u

frail egret
#

i have no Idea as I got these pics over telegram to help prepare for placement drive

crisp crown
#

what would u say are ways to improve this code?

frail egret
crisp crown
#

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?

frail egret
frail egret
#

this is more confusing for me

crisp crown
crisp crown
#

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

frail egret
crisp crown
#

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?

frail egret
crisp crown
#

design is the single thing that separates a junior from the senior

frail egret
#

it does ntg

crisp crown
frail egret
#

so the best among those 3 are switch or polymorphism

crisp crown
#

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?

frail egret
crisp crown
#

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

frail egret
crisp crown
#

how would that look like? could u write a quick snippet?

#
class Vehicle {
 // ...
}

class Car extends Vehicle {
 // ...
}
frail egret
crisp crown
#

how would polymorphism make it work better

#

please fill in the blanks of the above snippets

frail egret
errant stumpBOT
crisp crown
#

and what would u have in Vehicle?

frail egret
#

wait a sec

#
class Vehicle {
 carbonEmission(){
   return self.engineSize * self.tire; 
 }
}

class Car extends Vehicle {
 @override
 carbonEmission(){
   return self.engineSize * self.tire * 2.5; 
 }
}
errant stumpBOT
crisp crown
#

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

frail egret
#

this was very cool learning with u sir!

crisp crown
#

instead of having a centralized place that controls them all

#

ur welcome

#

now to ur second question

frail egret
#

this is literally out of my mind

crisp crown
#

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?

frail egret
#

it will make a looot of nesting then

#

similarly 2ed option is a business logic to change

crisp crown
#
if (user.experience > 6) {
  if (user.earnings >= 5000) {
    ...
  }
}
#

yes, nesting

#

does that help?

#

is it more readable?

frail egret
crisp crown
#

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

frail egret
#

4th adding a else at the end will have no change as else doesn't carry any condition

crisp crown
#

but yes

crisp crown
#
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

frail egret
#

ho!

#

visualizing the code matters

crisp crown
#

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

frail egret
#

dam

#

its super cool to learn those

#

thanks a loot sir

#

shall i close the Questions?

crisp crown
#

if u dont have any questions to that anymore right now, sure ๐Ÿ™‚

frail egret
#

thanks sir โค๏ธ