Showing posts with label CS2305 - PROGRAMMING PARADIGM. Show all posts
Showing posts with label CS2305 - PROGRAMMING PARADIGM. Show all posts

Thursday, August 21, 2014

CS2305 - PROGRAMMING PARADIGM

1. How can a subclass call a method or a constructor defined in a superclass?
Use the following syntax: super.myMethod(); To call a constructor of the superclass, just write super(); in the first line of the subclass’s constructor.

2. What’s the difference between a queue and a stack?
Stacks works by last-in-first-out rule (LIFO), while queues use the FIFO rule.

3. You can create an abstract class that contains only abstract methods. On the other hand, you can create an interface that declares the same methods. So can you use abstract classes instead of interfaces?
Sometimes. But your class may be a descendent of another class and in this case the interface is your only option.

4. What comes to mind when you hear about a young generation in Java?
Garbage collection.

5. What comes to mind when someone mentions a shallow copy in Java?
Object cloning.

6. If you’re overriding the method equals() of an object, which other method you might also consider?
hashCode()

Wednesday, August 20, 2014

CS 2305 - Programming Paradigm

1. Do I need to use synchronized on setValue(int)? 
It depends whether the method affects method local variables, class static or instance variables. If only method local variables are changed, the value is said to be confined by the method and is not prone to threading issues.

2. Do I need to use synchronized on setValue(int)? 
It depends whether the method affects method local variables, class static or instance variables. If only method local variables are changed, the value is said to be confined by the method and is not prone to threading issues.

3. What is the SwingUtilities.invokeLater(Runnable) method for? 
The static utility method invokeLater(Runnable) is intended to execute a new runnable thread from a Swing application without disturbing the normal sequence of event dispatching from the Graphical User Interface (GUI). The method places the runnable object in the queue of Abstract Windowing Toolkit (AWT) events that are due to be processed and returns immediately. The runnable object run() method is only called when it reaches the front of the queue. The deferred effect of the invokeLater(Runnable) method ensures that any necessary updates to the user interface can occur immediately, and the runnable work will begin as soon as those high priority events are dealt with. The invoke later method might be used to start work in response to a button click that also requires a significant change to the user interface, perhaps to restrict other activities, while the runnable thread executes.

4. What is the volatile modifier for? 
 The volatile modifier is used to identify variables whose values should not be optimized by the Java Virtual Machine, by caching the value for example. The volatile modifier is typically used for variables that may be accessed or modified by numerous independent threads and signifies that the value may change without synchronization.

5. Which class is the wait() method defined in? 
The wait() method is defined in the Object class, which is the ultimate superclass of all others. So the Thread class and any Runnable implementation inherit this method from Object. The wait() method is normally called on an object in a multi-threaded program to allow other threads to run. The method should should only be called by a thread that has ownership of the object’s monitor, which usually means it is in a synchronized method or statement block.

Tuesday, August 19, 2014

CS2305 - PROGRAMMING PARADIGM - Unit 4

1. Can you call one constructor from another if a class has multiple constructors
Yes. Use this() syntax.

2. Explain the usage of Java packages.
This is a way to organize files when a project consists of multiple modules. It also helps resolve naming conflicts when different packages have classes with the same names. Packages access level also allows you to protect data from being used by the non-authorized classes.

3. If a class is located in a package, what do you need to change in the OS environment to be able to use it?
You need to add a directory or a jar file that contains the package directories to the CLASSPATH environment variable. Let’s say a class Employee belongs to a package com.xyz.hr; and is located in the file c:/dev/com.xyz.hr.Employee.java. In this case, you’d need to add c:/dev to the variable CLASSPATH. If this class contains themethod main(), you could test it from a command prompt window as follows: c:\>java com.xyz.hr.Employee

4. What’s the difference between J2SDK 1.5 and J2SDK 5.0?
There’s no difference, Sun Microsystems just re-branded this version.

Monday, August 18, 2014

CS2305 - PROGRAMMING PARADIGM - Unit 4

1. How could Java classes direct program messages to the system console, but error messages, say to a file? 
The class System has a variable out that represents the standard output, and the variable err that represents the standard error device. By default, they both point at the system console. This how the standard output could be re-directed: 
Stream st =  new Stream (new  FileOutputStream ("techinterviews_com.txt")); 
System.setErr(st); 
System.setOut(st); 

2. What’s the difference between an interface and an abstract class? 
An abstract class may contain code in method bodies, which is not allowed in an interface. With abstract classes, you have to inherit your class from it and Java does not allow multiple inheritance. On the other hand, you can implement multiple interfaces in your class. 

3. Why would you use a synchronized block vs. synchronized method? 
Synchronized blocks place locks for shorter periods than synchronized methods. 

4. Explain the usage of the keyword transient?
This keyword indicates that the value of this member variable does not have to be serialized with the object. When the class will be de-serialized, this variable will be initialized with a default value of its data type (i.e. zero for integers). 

5. How can you force garbage collection? 
You can’t force GC, but could request it by calling System.gc(). JVM does not guarantee that GC will be started immediately. 

Sunday, August 17, 2014

CS2305 - PROGRAMMING PARADIGM - Unit 3

14 .What is the major difference between LinkedList and ArrayList? 

 LinkedList are meant for sequential accessing. ArrayList are meant for random accessing. 

15. What is nested class? 
If all the methods of a inner class is static then it is a nested class. 

 16. What is inner class? 
If the methods of the inner class can only be accessed via the instance of the inner class, then it is called inner class. 


 17. What is composition? 
 Holding the reference of the other class within some other class is known as composition. 

18. What is aggregation? 
It is a special type of composition. If you expose all the methods of a composite class and route the method call to the composite method through its reference, then it is called aggregation. 

19. What are the methods in Object? 
clone, equals, wait, finalize, getClass, hashCode, notify, notifyAll, toString 

21. Can you instantiate the Math class? 
You can’t instantiate the math class. All the methods in this class are static. And the constructor is not public. 

Tuesday, August 12, 2014

CS2305 - PROGRAMMING PARADIGM - Unit 1

1. How could Java classes direct program messages to the system console, but error messages, say to a file? 
 The class System has a variable out that represents the standard output, and the variable err that represents the standard error device. By default, they both point at the system console. This how the standard output could be re-directed: Stream st = new Stream(new FileOutputStream("output.txt")); System.setErr(st); System.setOut(st); 

2. What's the difference between an interface and an abstract class? 
 An abstract class may contain code in method bodies, which is not allowed in an interface. With abstract classes, you have to inherit your class from it and Java does not allow multiple inheritance. On the other hand, you can implement multiple interfaces in your class. 

3. Why would you use a synchronized block vs. synchronized method? 
 Synchronized blocks place locks for shorter periods than synchronized methods. 

4. Explain the usage of the keyword transient? 
 This keyword indicates that the value of this member variable does not have to be serialized with the object. When the class will be de-serialized, this variable will be initialized with a default value of its data type (i.e. zero for integers). 

5. How can you force garbage collection? 
You can't force GC, but could request it by calling System.gc(). JVM does not guarantee that GC will be started immediately.