answersLogoWhite

0

📱

Java Programming

The Java programming language was released in 1995 as a core component of the Java platform of Sun Microsystems. It is a general-purpose, class-based, object-oriented language that is widely used in application software and web applications.

5,203 Questions

Difference between instance and object?

Object - When there is an value.

int[] arr={1,2,3};

Instance - when there is a declaration for class with 'new'.

String arr=new String("test");

A obj2 = new A(); //this is the object instance with occupy memory for this..

For user defined classes, object and instances are same.

A static method may not be overriden to be non static?

Static methods can refer to instance variables and methods, as long as they are static as well. The reason you cannot mix nonstatic and static class members is because they have different scope. Static members are independent of any particular object, whereas nonstatic members are unique for each object that is instantiated. To clarify, consider the following class:

class A

{

int x=0;

static int y=1;

}

If three instances of A are created, 3 instances of x will also be created. Changing one of those x's has no effect on the others. However, only one instance of y will be created, regardless of how many A's are ever created. A change in y will be reflected in every A.

Now, if you were to call A.y+=x, which x would you be referring to? 3 A's have been created, each with possibly different values of x. Because of the ambiguity of this, you will get a compiler error whenever you mix static and nonstatic members.

Does java have goto?

Because goto statements usually result in hard to read code. This was a feature of C++ which the creators of Java decided they didn't want to allow.

What event started WW1?

The Blackout. I am doing a world war 2 project for school. The Blackout in World War 2 started on the 1st of September 1939, 2 days after the outbreak of the war. (WWII) I am still not sure when the Blackout ended. Good Luck with that, thingymabob
Septenber 1, 1939-September 2, 1945

What is AutoBoxing and UnBoxing in java 5?

Conversion of primitive data types automatically to their corresponding wrapper classes is called AutoBoxing and the reverse of this operation is called UnBoxing.

Ex:

Long L = new Long(100);

long x = L;

Now x will have a value 100.

What are the advantages of using an assembly programming language in comparison to using an Object Oriented programming language?

The main strength is that it gives complete control over the machine at the lowest possible level. The main weakness is that everything must be encoded in terms the machine can understand. For instance, the otherwise simple operation of x = y + z requires that we move the values stored at the addresses identified by y and z into the appropriate CPU user registers, then invoke the appropriate ADD instruction, then move the accumulator register value into the address identified by x. That's a lot of work for an otherwise simple operation.

What are runtime errors in c?

A run time error occurs when a compiled program executes, and during execution, the binary code attempts a task that is not permitted by the operating system or libraries. Divide by zero is an example of a run time error. When the executed program divides one variable by a second variable, and the second variable holds a value of zero, the run time library will issue a run time error. Since the compiler cannot predict the values held by the variables, it cannot prevent such an error from occurring, and so the run time library traps the run time error during program execution.

.

Some run time errors include:

- divide by zero

- no stack space

- memory reference out of bounds

- write protected file

Can main method be non-static?

No, the reason is simple:

A method marked as a static means, that you don't need create an instance (create an object with the 'new' operator) in order to use it.

The main method is the entry point for a java application, therefor there is nothing after you call it. no one who can create an object of the type of your class and call the main method. So the jvm must call the main method with no object reference.

Explain four major advantages of Object Oriented Programming with the help of an Example?

To explain an object oriented programming with real time examples, try using a packet of sweetener. A packet of sugar has a rectangular shape, made of paper, inked wording and contains something sweet. The same can be said for a packet of Splenda. They both have properties inherited from the abstract packet.

Examples of java?

The term "exception" means "exceptional condition" and is an occurrence that alters the normal program flow. A bunch of things can lead to exceptions, including hardware failures, resource exhaustion, and good old bugs. When an exceptional event occurs in Java, an exception is said to be "thrown." The code that's responsible for doing something about the exception is called an "exception handler," and it "catches and handles" the thrown exception.

Exception handling works by transferring the execution of a program to an appropriate exception handler when an exception occurs. For ex: Your website displays the users bank account details. Suddenly if the bank database is down, imaging how the user would feel if an abrupt error page with numerous lines of java exception messages spewed all over the screen? First of all, it would be embarrassing and second of all, the user will get annoyed. Instead, if your code catches this database down scenario and displays a graceful message on screen saying "We are currently experiencing some difficulties in our system. Kindly try after some time. Thank you" it would be much better isnt it? That is exactly why we have or rather why we need Exception Handling.

Write a java program to find product of two numbers using command line argument?

class Product

{

public static void main(String[] args)

{

int x=Integer.parseInt(args[0]);

int y=Integer.parseInt(args[1]);

int z=x*y;

System.out.println("product="+z);

}

}

How many catch blocks can you use with one try block with example?

A try block can have multiple catch blocks, each handling a different type of exception. Be careful about the order of laying the exceptions. Using Exception at the top will catch everything that is derived from it, thereby missing the other specfic exception catches. And there can also be only one finally block.

What is the purpose of using Java server faces?

Information in Java server faces can be found on the websites of the stores that sell servers that can use Java such as PCWorld business. More information can be found in the Britannica online website.

What is polymorphism explain and example?

Polymorphism is the use of different Types to declare a variable, it is closely related to the inheritance hierarchy. It is used to often hide information from the client concerning how a piece of code works, they should only concern themselves with how to use it.

For instance:

//Declaring lists

//Can declare like this

ArrayList list = new ArrayList();

//or to hide information on how the list is implemented

List list2 = new ArrayList();

//Therefore the user only needs to know the List interface's methods to interact, don't //have to be concerned about the ArrayList implementation.

Some syntax rules for polymorphism:

//You have to Declare using the same or higher level of inheritance than you are //instantiating the Object as

//i.e. Child class is a child of the Parent class (i.e. Child extends Parent)

//therefore, you can

//Declare as a Parent

Parent c1 = new Child(); // Example 1

//Declare as a Child

Child c2= new Child(); //Example 2

//But you cannot declare as a Child and instantiate as a Parent

Child thisIsWrong = new Parent(); //Example 3

A really good check to see if you have the Instantiation right is to say the Instantiating class name(the one after the "=")IS A Declaring class name(the one before the "=") , if the statement makes sense then it is legal. For example, example 1: Child is a Parent? true, legal. example 2: Child is aChild? legal. example 3: Parent is a Child? false, illegal.

Using Polymorphism

Polymorphism hides the child's methods from the user if the variable is declared with the parent, which is useful if you don't want the user messing with the child methods.

As with the top example, first example with the declaration as a Parent c1 cannot use the child methods unless you cast c1 into a child (i.e. ( (Child) c1).cry() ). But in example 2 the Child method could be used since c2 is essentially a Child.

How was the Java programming language discovered?

No one discovered it, it wasn't found hidden deep within an Aztec tomb, and then decoded - it was invented. By James Gosling, I think.

How do you repair java?

repair java

I have found that most errors come from conflicts with "Virus/Spyware Programs" when Java is set to auto update. I solved my problem by pausing or turning the VirusSpyware Protection off temporarily and downloading updates directly from Java. Go to your Control Panel and look for the Java icon ... click on it and you will see a tab to update along with settings for automatic updates with which you can change to manual.

Explain how try and catch is used to handle exception in java?

class CatchException

{ public static void main(String args[])

{

try

{

int j=45/0;

}catch(Exception exp){System.out.println("Exception caught "+exp.stacktrace());}

}

}

Why use final in java?

The use of final denotes that a variable is a constant. For example:

final int PI = 3.14;

makes PI a constant. This is useful because now PI can't be changed, even accidentally, by you, the program, another programmer, or a user. Once a value is final, it is permanent, and can be used as a constant.

Note: usually programmers capitalize the names of constants, just to make it clear that the value is a constant.

The break statement is required in the default case of a switch selection structure?

Ends the case statement. Without it, any code after where the break; is supposed to be will get executed as well until it does encounter a break; or the end of the switch.

Code Example:

char cTest = 'a';

switch(cTest) {

case 'a':

/* Code here gets executed. */

case 'b': /

/* Code here gets executed. */

case 'c':

/* Code here gets executed. */

break;

case 'd':

/* Code here won't be executed. */

default:

/* Code here won't be executed. */

}

C program to solve equation in runge kutta method?

PROGRAM :-

/* Runge Kutta for a set of first order differential equations */

#include

#include

#define N 2 /* number of first order equations */

#define dist 0.1 /* stepsize in t*/

#define MAX 30.0 /* max for t */

FILE *output; /* internal filename */

void runge4(double x, double y[], double step); /* Runge-Kutta function */

double f(double x, double y[], int i); /* function for derivatives */

void main()

{

double t, y[N];

int j;

output=fopen("osc.dat", "w"); /* external filename */

y[0]=1.0; /* initial position */

y[1]=0.0; /* initial velocity */

fprintf(output, "0\t%f\n", y[0]);

for (j=1; j*dist<=MAX ;j++) /* time loop */

{

t=j*dist;

runge4(t, y, dist);

fprintf(output, "%f\t%f\n", t, y[0]);

}

fclose(output);

}

void runge4(double x, double y[], double step)

{

double h=step/2.0, /* the midpoint */

t1[N], t2[N], t3[N], /* temporary storage arrays */

k1[N], k2[N], k3[N],k4[N]; /* for Runge-Kutta */

int i;

for (i=0;i

{

t1[i]=y[i]+0.5*(k1[i]=step*f(x,y,i));

}

for (i=0;i

{

t2[i]=y[i]+0.5*(k2[i]=step*f(x+h, t1, i));

}

for (i=0;i

{

t3[i]=y[i]+ (k3[i]=step*f(x+h, t2, i));

}

for (i=0;i

{

k4[i]= step*f(x+step, t3, i);

}

for (i=0;i

{

y[i]+=(k1[i]+2*k2[i]+2*k3[i]+k4[i])/6.0;

}

}

double f(double x, double y[], int i)

{

if (i==0)

x=y[1]; /* derivative of first equation */

if (i==1)

x= -0.2*y[1]-y[0]; /* derivative of second equation */

return x;

}

What is the C sharp programming language?

While I generally prefer "C" as a more robust language, I also consider anything that is released by Microsoft to be unreliable and likely to fail. Java is the product of Sun Microsystems, and has a very good reputation. While I'm not a Java programmer, I have heard nothing but good reports about Java.

AnswerThese two languages are functionally nearly identical. If one can do something, you can bet that the other can do it just as easily. Each has a huge library of built in packages full of classes to help you solve any problem you need to handle.

What is the use of StringBuilder in java?

The StringBuilder class was added in Java 5. It has exactly the same API as the StringBuffer class, except StringBuilder is not thread safe. In other words, its methods are not synchronized. Sun recommends that you use StringBuilder instead of StringBuffer whenever possible because StringBuilder will run faster. So apart from synchronization, anything we say about StringBuilder's methods holds true for StringBuffer's methods, and vice versa.

What is clipping in applet?

The Clipping applet gives you a place to store those text/image/url clippings that would other wise end up on your desktop.

A Clipping is the process of confining paint operations to a limited area or shape.

How do you write a c function to swap two variables without using a temporary variable?

You can swap values a and b without a temporary as follows:

a=a^b

b=a^b

a=a^b

(^ is the bitwise XOR operator)

To show how this works, let's consider the values a=180 and b=204. In binary, 180 is 10110100 while 204 is 11001100. Thus:

a = 10110100

b = 11001100

a^b means that we look at the corresponding bits in each input value and output a 1 if one and only one of the two input bits is set. Thus the output (o) is:

a = 10110100

b = 11001100

o = 01111000

In the expression a=a^b, the output of a^b is assigned to a, thus the values of a and b now look like this:

a = 01111000

b = 11001100

We then repeat the operation, a^b, this time assigning the output to b:

a = 01111000

b = 11001100

o = 10110100

So now we have:

a = 01111000

b = 10110100

We repeat the operation one more time, assigning the output value to a:

a = 01111000

b = 10110100

0 = 11001100

So now we have:

a = 11001100

b = 10110100

The two values have now been swapped.

To write this in C, we can use shorthand expressions using the compound XOR-ASSIGN operator (^=):

a^=b

b^=a

a^=b

These individual expressions can then be combined into a single expression:

a^=b^=a^=b

Finally, to use this expression in a C function, we need to pass a and b by reference (using pointer variables):

void swap (int* a, int* b) {

(*a)^=(*b)^=(*a)^=(*b);

}

Note that a and b must be of an integral type (char, short, long, int, unsigned, etc). The example above only works on type int. If we wish to swap other types, or more complex types, we must treat those values as if they were an integral type. One way to achieve this is to treat those types as if they were an array of type char, supplying the length of the arrays through a separate argument:

void swap (char* a, char* b, size_t size) {

for (int i=0; i<size; ++i) {

a[i]^=b[i]^=a[i]^=b[i];

}

}

For example, to swap two doubles, we can use the following call:

int main (void) {

double a, b;

a = 3.14;

b = 1.1;

swap ((char*) &a, (char*) &b, sizeof (double));

assert (a==1.1);

assert (b==3.14);

return 0;

}

Note that a and b must of the same type.