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

Java program to print 1 8 27 64.?

public class PrintCubes {

public static void main(String args[]){

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

int cube = i * i * i;

System.out.print(cube + " ");

}

}

}

What is the difference between program and programming?

define program

i. program means set of instruction which are required for a particular type of task is called program

ii. set of instruction or commands which are given for the computer to do different activity or jobs or works is called program

define programming

the process of creating/modifying a program; some people's job, others' hobby

What is special about the lang and string packages for the language Java?

The string can never be modified, they are immutabile. If you think a string is modified, it is actually a new creation. It's basically a conversion of other objects.

What do the c and v in argc and argv stand for?

The letter "c" in argc stands for count. The letter "v" in argv stands for vector. You can visit this website for more info.......... http://www.dgp.toronto.edu/~ajr/209/notes/argv.html

What is the BNF of the for loop?

FOR ::= for ( [EXPRESSION]; EXPRESSION;[EXPRESSION]) STATEMENT

note: FOR itself is a STATEMENT as well:

STATEMENT ::= ...| IF | ELSE | WHILE | FOR | ... | EXPRESSION; | EMPTY_STATEMENT; | COMPOUND-STATEMENT | ...

Draw the recursion tree for the merge-sort procedure on an array of 16 elements explain why memoization is ineffective in speeding up a good divide-and-conquer algorithm such as merge-sort?

The MERGESORT algorithm performs atmost a single call to any pair of indices

of the array that is being sorted. In otherwords, the subproblems do not

overlap and therefore memoization will not improve the running time.

otherwise........take the look at following:

It does not have the Overlapping Subproblems property.

(Not re-visiting subproblems.)

Needs lot of space to store solutions of subproblems.

Overlapping sub-problems property is as follows:

We accidentally recalculate the same problem twice or more.

Can you assign a char value to a variable of datatype of byte in java?

Not without casting. A char is a 16 bit type, whereas a byte is an 8 bit type. Therefore the compiler cannot guarantee that the 16 bit value will fit into the 8 bit value without overflowing. If you attempt to stick a char into a byte, you will get a compiler error. To override this, you can cast the char value to a byte during assignment. However, you might get some unexpected results. A few examples below:

char a = 'A';

byte b = a; //compiler error

char a = 'A';

byte b = (byte)a; //valid, no error. b=65

char a = 172;

byte b = (byte)a; //valid, no error, but b=-84 because of overflow.

Why do you need to create an instance?

Creating an instance is essential because it allows you to utilize a specific blueprint or class to generate a unique object that encapsulates its properties and behaviors. This enables the implementation of object-oriented programming principles, such as encapsulation and inheritance, facilitating code reuse and organization. Instances also allow for the manipulation of data and functionality specific to that object, improving modularity and maintainability in software development.

Example of constructor in java?

Every class, including abstract classes, MUST have a constructor. Hard Code that into your brain. But just because a class must have one, doesn't mean the programmer has to type it. A constructor looks like this:

class Car {

Car() { } // The constructor for the Car class

}

You notice anything missing in the declaration above? There's no return type! Two key points to remember about constructors are that they have no return type and their names must exactly match the class name. Typically, constructors are used to initialize instance variable state, as follows:

class Car {

int size;

String name;

Car(String name, int size) {

this.name = name;

this.size = size;

}

}

In the preceding code example, the Car class does not have a no-arg constructor. That means the following will fail to compile:

Car f = new Car(); // Won't compile, no matching constructor

but the following will compile:

Car f = new Car("Ford", 43); // No problem. Arguments match

// the Car constructor.

So it's very common for a class to have a no-arg constructor, regardless of how many other overloaded constructors are in the class (constructors can be overloaded just like methods)

Why converting a base class pointer to a derived class pointer is consider dangerous by the compiler?

There are two methods of casting one type to another: static casting and dynamic casting (both of which apply to pointers and references to objects). Statically casting a derived class to a base class is typesafe as the base class is guaranteed to exist if the derived class exists. However, static casting from a base class to a derived class is always considered dangerous as the conversion is not typesafe. Dynamic casting exists to cater for this scenario, however it is only possible when the base class is polymorphic (thus ensuring the required runtime information is available). If the conversion is not possible, the return value is NULL. However, it is considered bad programming practice to dynamically cast from a base class to a derived class. If the base class is polymorphic (which it must be), there is NEVER any need to dynamically cast between types. Virtual methods ensure correct behaviour. Whenever you are forced to dynamically cast from a base class to a derived class, consider redesigning the base class interface instead, as it is a clear sign of bad design. It is not dangerous, however; only static casting from a base class to a derived class is considered dangerous.

No of occurrence of element in an array?

The below is an example to find the instance of a string in a string array. You can modify this to any kind of array and also pass the array and value to be checked as parameters.

public class ArrayTest {

/**

* @param args

*/

public static void main(String[] args) {

String[] strArr = new String[] {"One", "Two", "Three", "Four", "One"};

String txt = "One";

int size = strArr.length;

int counter = 0;

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

String temp = (String) strArr[i];

if(temp.trim().equals(txt)){

counter++;

}

}

System.out.println("The word '" + txt + "' occurs " + counter + " Times in the array");

}

}

The output of this program would be:

The world 'One' occurs 2 Times in the array

What is the sum of 25?

The sum of a single number is the number itself. But usually the word "sum" is used for adding more than just one number.

How do you close a string literal?

Just as you have started it.

good examples:

'string'

"string"

`string`

»string«

bad examples:

'string"

"string`

»string'

How do you call private method outside the class?

You don't. The whole point of private methods or fields is that you can't access them directly from outside the class. You can call the public methods, and in some cases protected methods, and those might indirectly invoke the private methods.

How do you reverse a given string without using string functions?

1.take while loop in which u continue up to null in array of string.

2.once u get null pointer then take a new array of same length.

3.when u get null there ll save position no in variable i.

4.take while loop i to 0;

5.and j=0 for new array.

6.in above loop copy old array to new array in each cycle of loop.

7.j++;

8.u ll get reverse of string.

Does java class must have public method?

no you can have a class with no public methods and even with a a private constructor

public class Example {

//constructor

private Example(){

}

}

How do you write a program that outputs a given characters in reverse?

write the javascript code to display the reverse no. of given no. (e.g. 247 reverse of 742)

Which is a correct static method call of Math class method sqare root?

double root = Math.sqrt(XXXXX);

where XXXXX is the number for which you want to calculate the square root.

You want many conditions to be satisfied for a single while loop how can you do it?

You can put as many conditional tests as you want in the while loop conditional location.

As a rule, however, it is a better idea to simplify the conditions as much as possible so that the reader of the code has a better understanding of what is being accomplished.

Once you start putting multiple conditions in a while loop with ANDs and ORs together, the logic can get complex and not well understood.

When to Use Encapsulation?

The ability to make changes in your code without breaking the code of all others who use your code is a key benefit of encapsulation. You should always hide implementation details. To elaborate, you must always have your variables as private and then have a set of public methods that others can use to access your variables. Since the methods are public anyone can access them, but since they are in your class you can ensure that the code works the way that is best for you. So in a situation that you want to alter your code, all you have to do is modify your methods. No one gets hurt because i am just using your method names in my code and the code inside your method doesnt bother me much.

How do you call start method into run method?

In Java a Thread object has two methods that the programmer needs to know: start and run.

run is where we put the code that we want to execute when the Thread begins.

The start method is what tells the Java Virtual Machine that it should create a new thread and tell it to execute its run method.

While you can make a call to thread.run() in order to execute the code in the run method, this will not actually make a new thread in the JVM, but will execute just like any normal method call.

What class level is a BT4 considered?

In hazardous areas where explosive chemicals and reactions take place, motors with a class level of BT4 are considered ideal. Their tough exterior make them resistant to violent jostling.