Download Java Programming Concepts: Questions and Answers and more Exams Computer Science in PDF only on Docsity!
COMPUTER SCIENCE JAVA QUESTIONS AND
ANSWERS
keywords - ANSWER class, while and if are. primitive types - ANSWER which are simple values stored directly in the memory location reference types - ANSWER which store addresses of objects stored somewhere else in memory static - ANSWER methods not associated with any object instance - ANSWER methods are part of objects and can only be called through an object What are the two general ways of accessing a Java array? - ANSWER direct access and sequential access direct access - ANSWER accessing an arbitrary location sequential access - ANSWER going through the entire array compiler/syntax errors - ANSWER prevent program from being compiled properly,
forgetting semicolon or braces logic errors - ANSWER are problems with what the code actually does, like forgetting braces around else, so the code doesn't know what to do with else Data hiding - ANSWER when an object hides its internal data from code outside the class that the object //is an instance of. Only the class can access its own methods. Make fields private and methods public data abstraction - ANSWER using a class without knowing its implementation Details differences between array and arraylist - ANSWER - Array is a fixed length data structure
- arraylist- You can not change length of Array once created in Java but ArrayList re-size itself when gets full depending upon capacity and load factor.
- moreover, one big difference between ArrayList and Array is that you can't store primitives in ArrayList it only can contain Objects while Array in java can hold primitives as well as Objects infinite loops - ANSWER - kind of a loop which doesn't get over, that means its test condition will always true because nothing is being done inside the body to affect condition.
complete, the bubble sort returns to elements one and two and starts the process all over again. So, when does it stop? The bubble sort knows it is done when it looks at the entire array and no "swaps" are required (thus the list is in correct order). Linear Search - ANSWER - Start at the first of the array and look at each in order until the end of the array is reached or the element is discovered We say this is a linear run-time - or time proportional to N, the number of items in the array
- One stops the loop with failure (get to end)
- The other stops the loop with success (found item) What is a binary search? - ANSWER What is a binary search? - In computer science, a binary search or half-interval search algorithm finds the position of a specified input value (the search "key") within an array sorted by key value. For binary search, the array should be arranged in ascending or descending order.
- Searching for a given key, K Guess middle item, A[mid] in array If A[mid] == K, we found it and are done If A[mid] < K then K must be on right side of the array If A[mid] > K then K must be on left side of the array Either way, we eliminate ~1/2 of the remaining items with one guess Show on board for a search for 40
- Binary Search only works for arrays of ints
advantages of arrayList - ANSWER Arrays are of fixed length. You can not change the size of the arrays once they are created.
- You can not accommodate an extra element in an array after they are created.
- Memory is allocated to an array during it's creation only, much before the actual elements are added to it. Syntax of ArrayList - ANSWER import java.util.ArrayList; ArrayList stuff = new ArrayList(); stuff.add("foo"); stuff.add("bar"); garbage collection - ANSWER As long as an object is being referenced, the JVM considers it alive. Once an object is no longer referenced and therefore is not reachable by the application code, the garbage collector removes it and reclaims the unused memory. public - ANSWER - To access from class or from outside of an object, the data must be The this reference - ANSWER - Often in instance methods you are accessing both instance variables and method variables
- If a method variable has the same name as an instance variable, updates will change the method variable, NOT the instance variable
The AWT (Abstract Windowing Toolkit) - ANSWER - was developed for the first versions of Java Created components such as Frame, Panel, Button, TextField, Label JFrames - ANSWER JFrames - are objects that will be the windows in graphical applications We can draw/paint graphics within them We can place and manipulate graphical components within them JButtons - ANSWER - JButtons are simple components that can also show text on the display However, in addition to showing text, they also respond to clicks of the mouse
- If a user clicks the mouse within a JButton, an ActionEvent object is generated in response This object is automatically passed to an ActionListener object
- The ActionListener must be registered to "listen" to the JButton
- ActionListener is actually an interface with the single method actionPerformed() We will discuss interfaces formally soon JTextField - ANSWER This is a component in which the user can enter text
- When user presses "Enter", the component fires an ActionEvent
- Same event as fired by a JButton
- We can use it to respond to input from user
- For example to update the text of a JButton
- But, in that case the listener must have access to both the source of the event, the object doing the firing, (the JTextField) and the object to be modified, the JButton
- To do this the objects must be set up differently so that the variables can be shared mutator - ANSWER setter accessor - ANSWER getter super - ANSWER The super keyword in java is a reference variable that is used to refer immediate parent class object. Whenever you create the instance of subclass, an instance of parent class is created implicitly i.e. referred by super reference variable. superclass - ANSWER is the big boy class that can have methods and data
- there can be subclasses of the superclass subclass extends superclass superclass never interacts with subclass
- can have same method name
- the last argument will display abstract class - ANSWER - can restrict how much extra code you must write
- can go in put into the code without remaking the class interface - ANSWER - abstract superclass with no instance data What are wrapper classes? - ANSWER - wrapper classes are classes that wrap objects around primitive values thus making them compatible with other java classes
- we cant store an int in an array of Object, but we can store an integer wrapper classes are uppercased Integer, Float, Byte, Double, Boolean Autoboxing - ANSWER - is Java's process of automatically boxing up a primitive value inside an object Integer myInt=5;
the primitive value 5 is autoboxed and the address of the resulting object is sent to the myInt variable Integer.parseInt() - ANSWER is a static method that allows us to convert from a String into an int Checked exceptions - ANSWER If a method does NOT handle these, the method MUST declare that it throws them Done in a throws clause in the method header These include IOException, and InterruptedException (and their subclasses) Unchecked exceptions - ANSWER Method NOT needed to explicitly "throw" these These include RunTimeException and Error