Review Questions and Answers 1. Explain, in detail, the concept of a

advertisement
Review Questions and Answers 1. Explain, in detail, the concept of a variable. Why are they useful? o Variables are symbolic names given to some piece of information that affords us the ability to use that name, independently of the data it represents, to refer to that value. 2. Describe what a program is to a computer. o A program is simply a set of instructions for the computer to perform. 3. Explain the difference between syntax and semantics. Which errors are easier to find? o Syntax defines the grammatical rules of the language; semantics pertains to the meaning of the language. Syntax errors are much easier to find, since the compiler will find them before the program ever executes. Semantic errors can only be found at runtime. 4. Why do we need high-­‐level programming languages? o Programming languages are essential because computers can only understand binary, and it is quite difficult to code in 1s and 0s, programming languages abstract those binary digits into keywords and language constructs that are much closer to our natural language 5. What is a compiler? o A compiler converts your source code into machine language that the computer can understand. 6. What is an array? o An array is a variable type that can hold a fixed number of values of all the same type. 7. What are the limitations of an array? o Arrays must contain items of all the same type, and the size of the array is fixed. 8. How do we find the number of items in an array? o Reference the “length” property of the array: someArray.length 9. What is it called when we have arrays inside of arrays? o Arrays that consist of more arrays are termed “multi-­‐dimensional arrays” 10. What conventions do we follow in the naming of our variable and function names? o Camel case. Starting with a lowercase letter, all words within the name are started with a capital letter: numberOfCheeseburgers 11. Explain the difference between an array and an array list. o An array can only hold objects of all the same type and is a fixed size, array lists can consist of different types and items can be added and removed at runtime. 12. Explain the concept of a count-­‐controlled loop. o A count-­‐controlled loop is a concept of repeating code for some known number of times. For loops are geared to handle this task nicely. 13. Explain the concept of a condition-­‐controlled loop. o A condition-­‐controlled loop is a concept of repeating code until some condition becomes true. While loops are best suited for this. 14. Explain the concept of a collection-­‐controlled loop. o Collection controlled loops are a shorthand way of looping over each item in some collection, for example, an array or arraylist. 15. What is the process of taking one type and converting it to another also called? o Casting 16. What are the purposes of if, else statements? o If, else if, else statements allow our program to make decisions, branch into different paths of execution based on conditions. 17. What is a regular expression? o A regular expression is a tool we can use to match patterns instead of matching literal values 18. What would this regular expression match? [0-­‐9][A-­‐Z]+ o Any string that starts with one number, and has at least one uppercase letter following it, e.g.: 9CAT, 7B, 2JAVAISAWESOME 19. What is so special about our “main” method? o The main method is the entry-­‐point to our application 20. How do we print a line to standard error? o System.err.println(“Oops!”); 21. Explain the concept of packages in Java. o Packages help organize our code by grouping together classes that offer similar functionality 22. What is an advantage that Java can claim over some other compiled languages? o Instead of being compiled directly to machine code, Java is compiled to an intermediary language of a Java Virtual Machine, meaning you can compile your source code on any platform (e.g. Windows, Mac, Linux) and it will work on any other platform that has the Java Virtual Machine installed. 23. How do you pronounce my last name? o Sounds like “harmonica” 24. What would be the result of this computation: i++; o The value of “i” will increase by one. 25. Explain a situation in which an infinite loop might occur. o Infinite loops occur when a given condition that is the basis of our loop can’t ever be satisfied 26. What is the Boolean result of this statement: true && false o False. Both sides of an && (and) statement must be true in order for the statement to be true. 27. What is the Boolean result of this statement true || false o True. Only one side of the || (or) statement needs to be true in order for the whole statement to be considered true. 28. Explain the concept of conditional short-­‐circuiting. o If the first side of an && statement is false, or the first side of an || statement is true, the compiler need not check the other side, because the nature of these operators make it so the decision is already made. 29. What is the Boolean result of this statement false || false o False. Neither side of the statement is true, so the whole statement is false. 30. Why is it useful to be able to read/write from files? o Once we close our applications, all the values stored in our variables are gone. Storing those values in files on the hard drive allow us to maintain state in our application between uses. 31. Why do we use try/catches in our code? o Some code, like converting a string into numbers, has the possibility of throwing exceptions. In order for our program not to totally fail, we put these safeguards to “catch” the exception and handle it accordingly. 32. Why do we need to validate user input? o It is necessary to maintain data integrity. 33. Why do we need functions/methods? o Functions/methods come in handy when we can group together lines of code that all pertain to accomplishing a specific task, that way later we can simply call that method by name, rather than duplicating code. 34. What does the modulus operator do? o Returns the remainder. 35. What is a parameter/argument? o Parameters/arguments are values passed to a function to influence the way it executes. 36. What does it mean that a language is strongly typed? o Types are checked at compile time. 37. What are the advantages of strong variable typing? o It grants integrity to our variables, assuring that if we declare an integer variable, the only thing that can be stored inside is an integer. 38. What are the disadvantages of strong variable typing? o It can be a real pain in the you-­‐know-­‐where 39. Explain the concept of variable scope. o Functions/methods do not have access to variables declared within the bodies of other functions, and can only access variables declared within its own body, in the global scope, or passed in as arguments. 40. What is a purpose of object-­‐oriented programming/design? o To attempt to better represent real-­‐world objects in code. 41. What is a class? o The abstract idea of a type. A blueprint. What defines this type. 42. What two things are associated with a class? o State (things that describe the class) and behavior (things the class does) 43. What is an object? o An object is a particular instance of a class. 44. What is a constructor? o The constructor is the method that executes immediately upon instantiation of a new object. 45. What is an instance variable? o An instance variable is the programmatic means to represent the “state” of our object. 46. What does it mean for a method, variable, or constant to be declared as static? o Static methods, variables, and constants can be accessed without an instantiation of their containing class. 47. Why can’t we access or assign instance variables in static methods? o Static methods cannot be used in the context of an instantiation, so therefore it cannot “see” instance variables 48. Give an example of where we have used static methods. o Math.sqrt, Math.PI, System.out.println(); 49. Why might we want to have some methods, or constants declared to be static in our classes? o To organize our code, and create methods and constants that can pertain to our class in general. 50. What does it mean for a method to be overloaded? o An overloaded method is one that is defined more than once, that either has a different return type, number of parameters, ordering of parameters, or types of parameters. 51. What does it mean for a method to be overridden? o We can choose to write our own implementation of an inherited method by overriding it. 52. What does it mean for an operator to be overloaded? o An operator that is overloaded changes behavior based on the context in which it is used. For example, the “+” is used for both numerical addition and string concatenation. 53. What is a use case? o A use case diagram is a depiction of user goals for a given system. 54. Explain the difference between a value returning and a void function. o Value returning functions compute some value and then return that value back to its caller. Void functions don’t return a value, they just simply perform some action. 55. What is an abstract class? o An abstract class is a class that cannot be instantiated, but merely serves as a super-­‐class for sub-­‐typing. 56. What is an interface? o An interface is a template an implementing class must adhere to, they are 100% abstract, so no implementation details can be defined within an interface. 57. Explain the concept of inheritance. o Inheritance means that when we “extend” some super-­‐class in the form of some new sub-­‐class, we inherit its properties and methods. 58. Explain the concept of polymorphism. o The same object can be referenced as different types depending on the context. 59. What does GUI stand for? o Graphical User Interface 60. What is the name of the Java’s GUI framework we used? o Swing 61. Explain the concept of event-­‐handling. o Event-­‐handling pertains to the idea that some user-­‐event happens, e.g. a mouse click, and we create some code that triggers when that event occurs. 62. Explain a caveat of polymorphism (casting) o For example, when looping through an array containing a super-­‐type, those objects within will temporarily forget their type-­‐specific behavior unless they are casted back into their concrete forms. 63. What is the concept of “substring” o Substring allows us to return some portion of string. 64. What does it mean if I declare a method, variable, or class as final? o Final methods, variables, and classes can not be modified. 65. What is a constant? o Constants are like variables in that they link values to symbolic names, but the values of constants cannot be changed after they are defined. 66. What is the naming convention for a constant? o All uppercase letters, with underscores delimiting words FOR_EXAMPLE 67. Explain the difference between public and private visibility keywords. o Public and private are access modifiers. Any member marked as private can only be accessed by other members of the class, while public members can be accessed by anyone. 68. Why do we need getters and setters? o Getters and setters are used to set and retrieve the values of private instance variables, since otherwise external classes would be unable to use them. 69. What is the name of concept described in the previous question? o Encapsulation, or information hiding. 70. What does it mean for a type to mutable? o Mutable types cannot be changed after initialized. 71. Why do we mostly use layout managers rather than telling Java exactly where we want our GUI controls? o If we ever needed to add a control, we simply call the layout panel’s add() method, instead of computing exact pixel values to place this new control. 72. Explain the difference between a flow layout manager and a box layout manager. o Flow layout managers follow the “Z” pattern, laying out controls from left to right until running out of room, then starting on the next line moving left to right. Box layout managers lay one control out per line. 73. How might we parse a string into atomic parts if we know that each unit is separated by a constant delimiter? o Use that string’s split() method 74. Why can’t we just attach some method to be invoked when a button is clicked, why did we have to go through the trouble of creating those private classes? o Event-­‐handling is Java requires a class that implements the ActionListener interface, therefore we needed to create these classes. 
Download