Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and the Yahoo Answers website is now in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Anonymous
Anonymous asked in Computers & InternetProgramming & Design · 2 weeks ago

What are the correct answers to these interview questions I didn't get right for an entry level software developer position?

Question 1: When would you use a try and catch block vs. using an if statement?

My answer: A try and catch block is if you're trying to catch an error. An if statement is when you're trying to check if a condition is true or false. For example, if a care is red or blue.

I was told my answer was wrong

Question 2: What's the difference between an object and a class

My answer: An object represents a class. A class can contain multiple objects.

I was told my answer was wrong to this.

3 Answers

Relevance
  • Anonymous
    2 weeks ago

    Did it occur to you to ask the interviewers why they thought the answers were wrong? It is possible the interviewer was trying to see if you are willing to discuss differences of opinion, or if you were intimidated. And, if you were willing to discuss the differences, how you handled yourself in such a conversation. 

    A try - catch should be used to handle exceptional conditions. An if statement is used for normal control. 

    There might be circumstances where you have to use a try - catch block: You are using Java and writing a method "foo". "foo" calls a method that can throw an unchecked exception. The other programmers you are working with refuse to allow you to declare that "foo" throws that exception. To get "foo" to compile, you have to provide a try - catch.

    A class is a description or specification of an object or objects. A class is nothing by itself.*  Creating a class is creating a new data type. An object is an instance of that data type.

    * That isn't strictly true. A class might have static methods and static values. The Math class in Java is such a class. 

  • Anonymous
    2 weeks ago

    1) Isn't wrong. But could be better. You should say that if an error detected and 'thrown'.

    For example, with memory allocation failure. In C you'd check the pointer returned by alloc for being null with an if statement. In C++ you enclose the call to 'new' in a try block and catch any allocation failures. ( Believe it or not I've seen professional C++ programmers do it the wrong way. )

    2) Again. You seem to know what the difference is but just not using the correct wording. So you should have been asked to elaborate.An object is an instance of a class. Each object has the variables. The class methods are shared by all instances.

  • EddieJ
    Lv 7
    2 weeks ago

    1) There should be, but often isn't,  a function available to test if something would cause an error, so, sometimes the only practical way to test for it is to let it happen.

    However, what they *might* be looking for you to say is that a <try / catch> block can catch *unexpected* errors.  That's not exactly true, because if you didn't expect the possibility, you wouldn't have bothered to put in the <try / catch> -- although there are exceptions to that -- when you include it -- just in case.  But, they were probably wanting to hear you use the word "unexpected".

    2)  Classes don't contain objects.  A class is like a blueprint for an object.  So, you would say that an object is an "instance' of a class.  (<-- the answer)  No matter how many houses you build from a blueprint, the blueprint would NOT contain the houses.

    In a blueprint of a rectangular house the specific length & width would be on the blueprint, but for objects, those dimensions would be variables called properties.  So, in the analogy, where, in a real blueprint, there would be the number to represent the length, the class just says <length>.  And, also departing from the analogy, once a house is instantiated, the dimension won't change, the properties of object in a program generally CAN change.  However, the programming could be written to prevent that.  

Still have questions? Get your answers by asking now.