Which produces faster program execution a compiler or pure interpreter?
A compiled program would execute faster than an interpreter running the same code step by step.
Why return type is not considered while overloading a function?
YES only if: The return type in the child class is a sub-type of the return type of the parent class.
Ex:
public int getName() {} - Parent class method
public String getName() {} - Child class method
If we implement the above two methods in the class hierarchy we will get compilation errors. whereas if we try the below implementation it will work:
public Object getName() {} - Parent class method
public String getName() {} - Child class method
This is because String is a sub-type of Object and hence it works.
This is also called as Covariant Overriding.
Note: This feature is available only from Java 1.5. Earlier versions of Java expect overriding methods to have exactly the same return type as the super class method.
What does the finalize method do in java?
The "finalize" method is called when an object is removed from memory. If you need to do any cleanup (closing streams/files, displaying output, etc.) you can put it in an overridden finalize method.
What are the advantages of different java datatypes?
The Basic data types in Java are
The String type is an object.
To learn more about Java go to:
[http://java.sun.com/docs/books/tutorial/java/index.html]
How do you run midlet program?
You need a J2ME implementation. Is short this means you need either:
- an Java enabled mobile phone or other handset (i.e one that can play games)
- a mobile device emulator.
For an emulator look for Wireless Toolkit (WTK) on Sun Microsystems or in the Sony Ericsson SDK. Usually you use this toolkit to write and build the MIDlet to begin with.
Java does not stand for anything. Java is a programming language created by James Gosling and now developed by Sun Microsystems. The name was chosen at random. It may have something to do with staying awake late a night?
A source program is the program written in which language?
It is programming that is licensed in such a way that it provides the source code for everyone to view and suggest changes to, and is usually at no cost or a very lost cost to anyone that wants the program and its source code.
What is the Diff between Access Specifiers and Access Modifiers?
An Access Modifier is a key word in java that determines what level of access or visibility a particular java variable/method or class has. There are 4 basic access modifiers in java. They are:
1. Public
2. Protected
3. Default and
4. Private
Private is the most restrictive access modifier whereas public is the least restrictive. Default is the access protection you get when you do not specifically mention an access modifier to be used for a java object.
Difference between reference variable and instance variable?
An object is the actual storage space in memory in which some collection of data resides.
A reference variable is a variable which refers to the memory location of an object.
Look at the pseudocode below:
Object obj = new Object();
Here obj is the reference variable, and the data to which it refers is the object.
How overcome problem of NullPointerException?
A NullPointerException is thrown when you attempt to access an object reference which is null.
Example:
String str = null;
str.equals("x"); // <- NullPointerException
To fix it, you must read the error message and find out what object is null. Determining how to ensure that this object is not set to null in the future must be made on a case-by-case basis.
Yes, you can uninstall the Java Runtime Environment from Microsoft Windows.
If you are a user with sufficient privileges, you can use the "Add/Remove Programs" control panel to find and remove the "Sun Java" entry.
This will not harm your computer.
How are an objects instance variables initialized of a class has only a default constructor?
You have to explicitly initialise all instance variables, otherwise their values will be initialised to whatever happens to be in memory at the time they are instantiated.
Consider the following class which has no explicit initialisation:
#include <iostream>
class object
{
public:
object(){}
public:
const int getData( void ) const { return( m_data ); }
private:
int m_data;
};
int main()
{
object O;
printf( "O.m_data = %d\n", O.getData() );
return( 0 );
}
Example output:
O.m_data = -858993460
This is not particularly useful; we must ensure m_data is initialised before we access its value.
One way to initialise object::m_data is from within the body of the constructor:
object(){ m_data = 0; } // inefficient initialisation
However this is quite inefficient because object::m_data will have already be instantiated by the time we got to the constructor body. This is akin to the following C-style initialisation:
int x; // instantiate x
x = 0; // initialise x
When what we really want to do is:
int x = 0;
Or use the more formal construction semantics:
int x(0);
Both achieve the same thing. They both initialise the variable x at the point of instantiation, not after instantiation. Fortunately, C++ allows us to initialise all instance variables at the point of instantiation, via the constructor's initialisation section:
object():m_data(0){} // efficient initialisation
While initialising a single primitive data type in the constructor body isn't going to cause major problems in terms of efficiency, with more complex data types the inefficiencies can very quickly add up. Thus it is important to use the initialisation section as much as possible and to only use the body of the constructor when there is no option.
The initialisation section is particularly important when dealing with derived classes. Consider the following:
#include <iostream>
class base
{
public:
base():m_int(0){}
base(const base& object):m_int(object.m_int){}
public:
const int getInt( void ) const { return( m_int ); }
void setInt( const int data ) { m_int = data; }
private:
int m_int;
};
class derived : public base
{
public:
derived():m_float(0.0){}
derived(const derived& object):m_float(object.m_float){}
public:
const float getFloat( void ) const { return( m_float ); }
void setFloat( const float data ) { m_float = data; }
private:
float m_float;
};
int main()
{
derived d;
d.setInt( 1 );
d.setFloat( 2.0 );
printf( "d.getInt() = %d, d.getFloat() = %f\n", d.getInt(), d.getFloat() );
derived c(d); // call copy constructor.
printf( "c.getInt() = %d, c.getFloat() = %f\n", c.getInt(), c.getFloat() );
return( 0 );
}
Example output:
d.getInt() = 1, d.getFloat() = 2.000000
c.getInt() = 0, c.getFloat() = 2.000000
Note that c should be an exact copy of d, but there's clearly a difference in the integer variables inherited from the base class. This is because the derived class copy constructor called the base class default constructor, not the base class copy constructor as you might have expected. To resolve this we must explicitly call the base class copy constructor and the only way to do so is via the initialisation section of the derived class:
derived(const derived& object):base(object),m_float(object.m_float){}
Note that the derived class' copy constructor now includes a call to base(object) in the initialisation section. This ensures the base class copy constructor is called. Although object is actually an instance of derived, because it derived from base it is also a kind of base, thus the call is legitimate. Now if we run the code we'll get the expected result:
d.getInt() = 1, d.getFloat() = 2.000000
c.getInt() = 1, c.getFloat() = 2.000000
As your classes become more and more complex you will inevitably find yourself creating overloaded constructors to provide a wide variety of initialisation methods. But keep in mind that it costs absolutely nothing to use the initialisation sections even if several constructors end up using the exact same initialisations. And while it is tempting to move all of the common initialisation code into a private method of the class and have each constructor call that method (known as a construction helper function), this simply adds yet more inefficiency by introducing yet another unnecessary function call. Helper functions such as these are undoubtedly useful during the initial development of a class but as soon as the class is finalised, get rid of the helper function and move all that functionality into the initialisation sections where they belong. In the case of derived classes, base class constructors will be called whether you like it or not, so it makes sense to call the most appropriate base class constructor from within the initialisation section of each of your derived class constructors. Remember that if you do not specify a base class constructor, its default constructor will be called implicitly, which may or may not be the most appropriate constructor in all cases (not least in the case of the copy constructor).
Fibonacci series with do while in java?
#include
#include
void main()
{
int a=0,b=1,c,i=2,n;
clrscr();
printf("enter no");
scanf("%d",&n);
if(n==0)
printf("%d\n",a);
else
printf("%d\n %d\n",a,b);
while(i<=n) {
c=a+b;
printf("%d",c);
a=b;
b=c;
i++;
}
getch();
}
What is object in programming?
Object in context of a class is the root of the class hierarchy in Java in the java.lang package.
Every class has Object as a superclass whether it's explicit or not. All objects, including arrays, implement the methods of this class.
Explain what is meant by object-oriented concept of abstraction?
Data abstraction is the means by which we lift the level of abstraction away from the machine (low-level) and closer to the application domain (high-level). As far as the machine is concerned, all data is binary, however the exact same binary representation can mean entirely different things within different contexts. Abstraction allows us to separate these contexts and thus give much greater meaning to the underlying representation.
Java code to find the sum in all integer?
The following will sum all integers from 1-100. Adjust according to your needs.
int sum = 0;
for (int i = 1; i <= 100; i++) sum += i;
System.out.println("The sum is " + sum);
The following will sum all integers from 1-100. Adjust according to your needs.
int sum = 0;
for (int i = 1; i <= 100; i++) sum += i;
System.out.println("The sum is " + sum);
The following will sum all integers from 1-100. Adjust according to your needs.
int sum = 0;
for (int i = 1; i <= 100; i++) sum += i;
System.out.println("The sum is " + sum);
The following will sum all integers from 1-100. Adjust according to your needs.
int sum = 0;
for (int i = 1; i <= 100; i++) sum += i;
System.out.println("The sum is " + sum);
When to use a constructor in java?
Private constructors generally have one of two uses.
The first is for implementing the singleton design pattern, in which you only ever want one instance of the class in existence. So you don't want new instances of the class being generated over and over. You do something like:
class Singleton {
// All classes can access this instance of the class and no other.
public static instance = new Singleton();
private Singleton() {
}
}
The second is for limiting the creation of classes which contain only static methods or constants.
Of course, private constructors are useful any time you want to limit the way other classes/packages/programmers interact with your class.
Why method inner classes can only access final variables from method?
I think the main reason is because the inner class may "outlive" the method in which it was created. If this happens, then the memory storing those variables would be lost and we would run into problems when trying to access them. If the variables are defined as final, then Java knows that those values will never change and it can move/copy them when the class is created.
Write a program to calculate the sum of n numbers using array?
int myArray[N]; //where N is a constant
int mySum = 0;
...
for (int arrayIndex1 = 0; arrayIndex1 < N; arrayIndex1++)
{
cin >> myArray[arrayIndex1];
}
...
for (int int arrayIndex2 = 0; arrayIndex2 < N; arrayIndex2++)
{
mySum +=myArray[arrayIndex2];
}
...
You mean source-file? The simplest format is one single main function:
int main (void)
{ puts ("Hello, World"); return 0; }
Example of procedural programming language and object oriented programming language?
example of procedural programming are those programming language that have structure e.g basic,fortran,c++,c and pascal e.t.c
Disadvantages of multiple inheritance?
If a single class inherits from two other classes, each of which has the same function implemented, how can we tell which one to call? This ambiguity often leads to non-deterministic behavior of classes in multiple inheritance languages.
How does one set a timer in Java?
To create a timer you might try the java.util.Timer class.
Check out the following methods:
void schedule(TimerTask task, long delay, long period)
Schedules the specified task for repeated fixed-delay execution, beginning after the specified delay.
void scheduleAtFixedRate(TimerTask task, long delay, long period)
Schedules the specified task for repeated fixed-rate execution, beginning after the specified delay.
If you just want a one-time event you can still use a Timer instance and schedule a single TimerTask or just create a new Thread with anonymous Runnable class that sleeps for 3 minutes before doing something.
new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(TimeUnit.MINUTES.toMillis(3));
// do stuff here
} catch (InterruptedException e) {
// handle interruption
}
}
}).start();