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

Write a program to copy the value of one string variable to another variable?

You can have two String variables (note that String variables are object references) refer to the same String object like so:

String str1 = "Hello";

String str2 = str1;

Now the str1 and str2 are references for the same String object containing the word "Hello".

If you actually want a new String object with a copy of the contents of the original String, you use the String constructor that takes a String argument, like so:

String str3 = new String(str1);

Now str1 and str3 refer to SEPARATE String objects that happen to contain the same sequence of characters (the word "Hello").

Since Strings objects in Java are immutable, they can be shared without worrying about the contents used by one variable being upset by the use through another variable as might happen with char[] arrays in C or C++ so the first method is probably sufficient for most cases.

What is NumberFormatException in java?

NumberFormatException is a subclass of the RuntimeException class. An object of type NumberFormatException is thrown when an application attempts to convert a string (that does not represent a number) into a numeric type.

What is the difference between schedule and class?

schedule is the thickness of the pipe and class is pressure/temperature rating of a fitting or pipe

How do you create an static int as an instance variable?

You create a static integer as an instance variable by declaring it in a class using a statement that consists of the privacy level, then the keywords "static" and "int", and finally, the name of the variable. For example:

public class TheAnswerIsHere {

public static int example = 0;

}

will define an int example with initial value 0. The variable is accessed through the statement TheAnswerIsHere.example.

Java null pointer exceptions?

A null pointer exception in java comes when you are trying to perform any action on an object that isnt initialized/has a value i.e., is a NULL Value

Ex:

private String s; //declare a string

if(s.equals("test")){

//do something..

}

You will get a null pointer in the if condition because you are checking a value that is null which is not allowed..

What is parse Int?

parseInt() is a method in the Integer class in Java that is used for parsing string values as numbers.

int i = Integer.parseInt("10");

would result in i being assigned a value of 10

Write a program to illustrate bitwise operators without swap?

#include<stdio.h> int main() { int n,n2; printf("enter the no. < 15 "); // here i am considering the case of 4 bits. (1111) binary = (15) decimal scanf("%d",&n); n2=n^10; /* 10 = 1010 in binary form, to invert its even bits , we will use bit wise XOR (^) operator 1010 has 1 at its even places, so it will invert the even bits of n. if there is any further problem mail me at buntyhariom@gmail.com www.campusmaniac.com */ printf("\n%d",n2); return 0; }

What is the java program for Dijkstra's algorithm?

import java.util.List;

import java.util.ArrayList;

import java.util.Collections;

class Vertex implements Comparable<Vertex>

{

public final String name;

public Edge[] adjacencies;

public double minDistance = Double.POSITIVE_INFINITY;

public Vertex previous;

public Vertex(String argName) { name = argName; }

public String toString() { return name; }

public int compareTo(Vertex other)

{

return Double.compare(minDistance, other.minDistance);

}

}

class Edge

{

public final Vertex target;

public final double weight;

public Edge(Vertex argTarget, double argWeight)

{ target = argTarget; weight = argWeight; }

}

public class Dijkstra

{

public static void computePaths(Vertex source)

{

source.minDistance = 0.;

PriorityQueue<Vertex> vertexQueue = new PriorityQueue<Vertex>();

vertexQueue.add(source);

while (!vertexQueue.isEmpty()) {

Vertex u = vertexQueue.poll();

// Visit each edge exiting u

for (Edge e : u.adjacencies)

{

Vertex v = e.target;

double weight = e.weight;

double distanceThroughU = u.minDistance + weight;

if (distanceThroughU < v.minDistance) {

vertexQueue.remove(v);

v.minDistance = distanceThroughU ;

v.previous = u;

vertexQueue.add(v);

}

}

}

}

public static List<Vertex> getShortestPathTo(Vertex target)

{

List<Vertex> path = new ArrayList<Vertex>();

for (Vertex vertex = target; vertex != null; vertex = vertex.previous)

path.add(vertex);

Collections.reverse(path);

return path;

}

public static void main(String[] args)

{

Vertex v0 = new Vertex("Harrisburg");

Vertex v1 = new Vertex("Baltimore");

Vertex v2 = new Vertex("Washington");

Vertex v3 = new Vertex("Philadelphia");

Vertex v4 = new Vertex("Binghamton");

Vertex v5 = new Vertex("Allentown");

Vertex v6 = new Vertex("New York");

v0.adjacencies = new Edge[]{ new Edge(v1, 79.83),

new Edge(v5, 81.15) };

v1.adjacencies = new Edge[]{ new Edge(v0, 79.75),

new Edge(v2, 39.42),

new Edge(v3, 103.00) };

v2.adjacencies = new Edge[]{ new Edge(v1, 38.65) };

v3.adjacencies = new Edge[]{ new Edge(v1, 102.53),

new Edge(v5, 61.44),

new Edge(v6, 96.79) };

v4.adjacencies = new Edge[]{ new Edge(v5, 133.04) };

v5.adjacencies = new Edge[]{ new Edge(v0, 81.77),

new Edge(v3, 62.05),

new Edge(v4, 134.47),

new Edge(v6, 91.63) };

v6.adjacencies = new Edge[]{ new Edge(v3, 97.24),

new Edge(v5, 87.94) };

Vertex[] vertices = { v0, v1, v2, v3, v4, v5, v6 };

computePaths(v0);

for (Vertex v : vertices)

{

System.out.println("Distance to " + v + ": " + v.minDistance);

List<Vertex> path = getShortestPathTo(v);

System.out.println("Path: " + path);

}

}

}

What is the function of a keyword suggestion tool?

The function of a keyword suggestion tool is to assist in finding relevant and popular keywords for online content. It analyzes search data and provides suggestions for keywords that users are likely to search for. This helps optimize content for search engines and improves visibility and organic traffic to websites.

How did copy string one variable to another variable in body of the program in c?

  1. /*Program to Copy one string to another using pointer.*/
  2. #include
  3. #include
  4. main()
  5. {
  6. char a[80],b[80],*pa,*pb;
  7. int i=0;
  8. clrscr();
  9. printf("Given first string ");
  10. scanf("%s",a);
  11. pa=&a[0];
  12. pb=&b[0];
  13. while(*pa!='\0')
  14. {
  15. *pb=*pa;
  16. pa++;
  17. pb++;
  18. }
  19. *pb='\0';
  20. printf("\nCopied string is");
  21. puts(b);
  22. }

How do you write c program to identify keywords using transition table?

#include

#include

#include

void keyw(char str[10])

{

if(strcmp("for",str)==0)

printf("%s is a keyword",str);

else if(strcmp("while",str)==0)

printf("%s is a keyword",str);

else if(strcmp("char",str)==0)

printf("%s is a keyword",str);

else if(strcmp("int",str)==0)

printf("%s is a keyword",str);

else if(strcmp("if",str)==0)

printf("%s is a keyword",str);

else if(strcmp("else",str)==0)

printf("%s is a keyword",str);

else

printf("%s is an identifier",str);

printf("\n");

}

main()

{

FILE *f1,*f2,*f3;

char c,str[10];

int num[100],ln=0,tvalue=0,i=0,j=0,k=0;

printf("Enter a C program expression");

f1=fopen("input.c","w");

while((c=getchar())!=EOF)

fputc(c,f1);

f1=fopen("input.c","r");

f2=fopen("identifier.txt","w");

f3=fopen("specialchars.txt","w");

while((c=fgetc(f1))!=EOF)

{

if(isdigit(c))

{

tvalue=c-'0';

c=fgetc(f1);

while(isdigit(c))

{

tvalue=tvalue*10+c-'0';

c=fgetc(f1);

}

num[i++]=tvalue;

ungetc(c,f1);

}

else if(isalpha(c))

{

fputc(c,f2);

c=fgetc(f1);

while((isdigit(c))isalpha(c)c==' 'c=='$')

{

fputc(c,f2);

c=fgetc(f1);

}

fputc(' ',f2);

ungetc(c,f1);

}

else if(c==' 'c=='\t');

else if(c=='\n')

ln++;

else

fputc(c,f3);

}

fclose(f1);

fclose(f2);

fclose(f3);

printf("Numbers in the program are \n");

for(j=0;j

printf("%d",num[j]);

printf("\n");

f2=fopen("identifier.txt","r");

k=0;

while((c=fgetc(f2))!=EOF)

{

if(c!=' ')

str[k++]=c;

else

{

str[k]='\0';

keyw(str);

k=0;

}

}

fclose(f2);

f3=fopen("specialchars.txt","r");

printf("The special chars in the pgm are \n");

while((c=fgetc(f3))!=EOF)

printf("%c",c);

printf("\n");

fclose(f3);

}

What is the use of Bitwise operators?

Answer

The bitwise operators treat a number as its binary equivalent rather than as a simple boolean value.

For most programming languages, a value of zero is considered FALSE and all other values are TRUE

Thus, 8 AND 11 returns TRUE as does 3 OR 0

In bitwise analysis, each binary bit of the digit are compared. The number of bits compared will depend on the type of number.

In C, a CHAR is usually 8 bits and can hold the binary numbers 0 to 255.

If we compare 8 (00001000) and 19 (00010011) with bitwise operators, we get different results from Boolean operators:

8 BITWISE AND 19 returns 0 (each bit in the response is set to 1 if both equivalent bits compared are 1) but 8 BITWISE OR 19 will return 27.

The utility of these methods is in identifying binary data. For example, all files on a PC have the characteristics 'Hidden' 'Read Only' 'Archive' and 'System' which can be set or unset using bitwise operations on a single byte of data. In truth this is a throwback to the days of small memory capacities where saving the odd byte was essential.

There are more uses of bitwise, especially in graphics, where XOR can be used to paint a sprite image to display it and then be used again to return a background to its former settings. I regret I lack the skill to explain this better.

What does the percent mean in Java?

The percent sign in Java is the modulus operator. Modulus is used to find the remainder from a division

For example if you did

int x = 10 % 6;

x would be equal to 4. 10 divided by 6 has a remainder of 4. The modulus operator can be used to determine whether a number is divisible by another among other things.

In java public static voidmain function denotes what?

Java's main function denotes the entry point into the execution of your program.

What is pack method in java programming?

If your talking about when you are packing components in a JFrame for example, something along the syntax of:

public static void main(String[] args) {

GUIFrame frame = new GUIFrame();

frame.setLocation(100, 100);

frame.setTitle("This is a JFrame implementation");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.pack();

frame.setVisible(true);

}

If you mean something like this, then the pack() method simply crams all the components together, to make the smallest space in a frame. They can be useful if all you want is that, otherwise options such as the .setSize(int height, int length); are also available for specific sizes.

Why do you need java for Facebook?

I am not sure whether you actually need it, perhaps you do. The point is, if any Web site has Java applets - components programmed for Java - then the user must install Java to make it work. Java (and alternative technologies, such as Flash, or SilverLight) make it possible to make "richer" Web pages, i.e., Web pages with more options, and a better visual presentation.

I am not sure whether you actually need it, perhaps you do. The point is, if any Web site has Java applets - components programmed for Java - then the user must install Java to make it work. Java (and alternative technologies, such as Flash, or SilverLight) make it possible to make "richer" Web pages, i.e., Web pages with more options, and a better visual presentation.

I am not sure whether you actually need it, perhaps you do. The point is, if any Web site has Java applets - components programmed for Java - then the user must install Java to make it work. Java (and alternative technologies, such as Flash, or SilverLight) make it possible to make "richer" Web pages, i.e., Web pages with more options, and a better visual presentation.

I am not sure whether you actually need it, perhaps you do. The point is, if any Web site has Java applets - components programmed for Java - then the user must install Java to make it work. Java (and alternative technologies, such as Flash, or SilverLight) make it possible to make "richer" Web pages, i.e., Web pages with more options, and a better visual presentation.

How many class of Hierarchical Inheritance?

There are 2 main types of Hierarchical Inheritance - Single and Multi Level

Class A extends Class B - Single

Class A extends Class B which in turn extends Class C - Multi level.

Actually there is no limit to the number of levels till which you can inherit classes in multi level inheritance. But it is preferable to keep it at around 3 or 4 for ease of maintenance and understanding.

Can you use business logic in constructor with example in java?

No. Logic should never go in a constructor; constructors should only be used to instantiate and initialize object data.

What is the use of 'compareTo' in java?

To compare the contents of two objects. The equality, "=", will simply tell you whether two objects are in the same memory location; it doesn't tell you whether their contents are actually the same. To compare the contents, there is an equals() method which tells you whether the contents are actually equal; there is also a compareTo() which can also tell you which of two objects is "greater". Since objects can be quite complex, the programmer may have to overwrite the (often useless) default behavior of these methods.

What is a standard number?

Standard Number is a basic/regular number (Examples: 2, 4.2, 7000)