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

Why you are mark a class protected for inheritance?

Public, Private and Protected "keywards/ access modifiers" are used similarly as they are with variables. Protected variables, methods or class CAN ONLY be used by an inherited class.

What method gets a value from a class s field but does not change it?

An accessor method can be used to get a value from a class without changing the value.

How do you add two binary numbers using java code?

public class Exercise6{

public static void main(String[] args) {

// TODO Auto-generated method stub

System.out.println("Example: " + addBinaryNumbers("100", "111")); // Result = 1011

// Test Cases :

// Valid Equivalence Class

// Since the inputs are valid, there will be no errors

assert (addBinaryNumbers("1", "1").equals("10") ) : "Valid Case 1: Error";

assert (addBinaryNumbers("10", "10").equals("100") ) : "Valid Case 2: Error";

assert (addBinaryNumbers("111", "111").equals("1110") ) : "Valid Case 3: Error";

assert (addBinaryNumbers("1110", "1011").equals("11001") ) : "Valid Case 4: Error";

// Invalid Equivalence Class

// Since the inputs are invalid, there will be errors

assert (addBinaryNumbers("2", "3").equals("101") ) : "Invalid Case 1: Error";

}

public static String addBinaryNumbers(String n1, String n2) {

String result = "";

for(int i = 0; i < n1.length(); i++) {

if ( (n1.charAt(i) != '0') && (n1.charAt(i) != '1') ) {

System.out.println("First number is invalid");

}

}

for(int i = 0; i < n2.length(); i++) {

if ( (n2.charAt(i) != '0') && (n2.charAt(i) != '1') ) {

System.out.println("Second number is invalid");

}

}

int number1 = Integer.parseInt(n1, 2);

int number2 = Integer.parseInt(n2, 2);

int resultNumber = number1 + number2;

result = Integer.toBinaryString(resultNumber);

return result;

}

}

What complications are imposed if one tries to implement a dynamic list using a traditional homogeneous array?

in dynamic list after adding a new element size of the list disturbed so deletion and insertation is necessary to add a new element

Explain the four basic data type in C. What is the size in byte of each type in DOS and UNIX platforms?

1. 'Explain' is not a question, but here you are:

unsigned char: a number between 0 and 255

signed char: a number between -128 and 127

unsigned short: a number between 0 and 65535

signed short: a number between -32768 and 32767

2. sizeof (type) will tell you

What is meant by lightweight in java?

LIght Weight Programming means the coding should be small .

What is serializable?

Ideally, transactions should be serializable. Transactions are said to be serializable if the results of running transactions simultaneously are the same as the results of running them serially-that is, one after the other. It is not important which transaction executes first, only that the result does not reflect any mixing of the transactions.

For example, suppose transaction A multiplies data values by 2 and transaction B adds 1 to data values. Now suppose that there are two data values: 0 and 10. If these transactions are run one after the other, the new values will be 1 and 21 if transaction A is run first, or 2 and 22 if transaction B is run first. But what if the order in which the two transactions are run is different for each value? If transaction A is run first on the first value and transaction B is run first on the second value, the new values are 1 and 22. If this order is reversed, the new values are 2 and 21. The transactions are serializable if 1, 21 and 2, 22 are the only possible results. The transactions are not serializable if 1, 22 or 2, 21 is a possible result.

So why is serializability desirable? In other words, why is it important that it appears that one transaction finishes before the next transaction starts? Consider the following problem. A salesman is entering orders at the same time a clerk is sending out bills. Suppose the salesman enters an order from Company X but does not commit it; the salesman is still talking to the representative from Company X. The clerk requests a list of all open orders and discovers the order for Company X and sends them a bill. Now the representative from Company X decides they want to change their order, so the salesman changes it before committing the transaction. Company X gets an incorrect bill.

If the salesman's and clerk's transactions were serializable, this problem would never have occurred. Either the salesman's transaction would have finished before the clerk's transaction started, in which case the clerk would have sent out the correct bill, or the clerk's transaction would have finished before the salesman's transaction started, in which case the clerk would not have sent a bill to Company X at all.

Write a program in C?

Yes, please do. Here is an example:

#include <stdio.h>

int main (void)

{ puts ("Hello, World"); return 0; }

How big in bytes are each of the 6 numeric primitive data types?

  • boolean - 1 bit (1/8 bytes) - The actual storage requires for a boolean type is not defined for Java implementations.
  • byte - 1 byte
  • short - 2 bytes
  • int/char - 4 bytes
  • long - 8 bytes
  • float - 4 bytes
  • double - 8 bytes

What is default object in java?

I don't think there is such a thing as a "default object". The default class, for inheritance purposes, is called "Object".

Write a C program to find the sum of all prime numbers in an array?

Borrowing the isPrime function from another answer of mine. Note that this will result in terrible performance for large arrays of numbers. (You should look into one of the sieve algorithms for this)

// returns 1 if n is prime, 0 otherwise

void isPrime(const int n) {

// We know that if n is composite, then we will find a factor in the range [2,sqrt(n)]

// so we compute the square root only once to limit our number of calculations.

const int sqrt_n = sqrt(n);

// Iterate through possible factors

int i;

for( i = 2; i <= sqrt_n; ++i ) {

// If n is divisible by i (n%i==0) then we have a factor

if(!(n % i)) {

return 0;

}

}

// If we get here, we know n has no factors other than itself and 1

return 1;

}

// returns the sum of all prime numbers in nums

int findPrimeSum(const int numsLength, const int[] nums) {

int sum = 0;

// iterate through nums and add up all the primes

int i;

for(i = 0; i < numsLength; ++i) {

if( isPrime(nums[i] ) {

sum += nums[i];

}

}

return sum;

}

What is the task of main method in a java program?

The main method is simply an entry point to your executable code. When you run a Java program, it looks for a main method as the first section of code to execute, which is why all of your programs start there.

What is the work of compiler?

A compiler converts a program in one programming language into a program in another programming language. Often the conversion is into a language that can be understood directly by the hardware.

What is a do-while loop?

A while loop evaluates the conditional expression at the start of each iteration, whereas a do..while loop evaluates the conditional expression at the end of each iteration. Thus the do..while loop always executes at least one iteration.

Write a program in java to print letters of a String in steps?

/* INPUT: Holy Home

* OUTPUT:H

* ------------ o

* --------------l

* ---------------y

* ---------------- _______________//The '-' are spaces

* ------------------H

* --------------------o

* ---------------------m

* -----------------------e

*

*/

import java.io.*;

class StyleString

{

protected static void main()throws IOException

{

BufferedReader in=new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter the String: ");

String s=in.readLine();

for(byte i=0;i<s.length();i++)

{

System.out.println();

for(byte j=0;j<i;j++)

System.out.print(' ');

System.out.print(s.charAt(i));

}

}

}

Where can one find more information about String Class Java?

There are many places one can go to learn more about the String Class in the programming language data. Stack Overflow is a great resource for any coding problems and is a good starting point.

Both Java and C plus plus compile source code to object code yet Java is called an interpreted language while C plus plus is called a compiled language explain this notion?

It can easily be explained by the fact that Java does not compile to object code. Java compiles to byte code that is suitable for interpretation by the Java virtual machine. C++ compiles to native machine code and therefore does not require interpretation of any kind. As a result, C++ programs run somewhat quicker than equivalent Java programs. However, because Java is interpreted, it is highly portable. Any machine with a Java virtual machine implementation (which is pretty much every device today) can run Java programs built from a single compilation. C++ requires that the code be written specifically for each platform and that the source be compiled separately upon each supported platform.

Which is more important class or object and why and which comes first?

Class is obviously more important than an object because an object is an instance of a class. A class may contain many objects, all of which are instances of that particular class. Class is also called the object factory because it contains all the statements needed to create an object and its attributes. A class also contains the statements that describe the operations that the created objejct will be able to perform. Therefor a class is more important and obviously come first.

How many constructors are there for the string class?

The String class has multiple Constructors. Some of them are:

1. String - new String(String val)

2. Character Array - new String(char[] array)

3. Character Array with index positions - new String(char[] array. int start, int end)

Difference between advanced java and core java?

There is nothing like core and advanced java. Actually SUN Corporation released three versions(editions) of Java. 1. Standard edition(J2SE). 2. Enterprise edition(J2EE). 3. Mobile edition(J2ME). Core and Advanced Java are the names given by some people which is wrongly meant. Second Answer These terms aren't an actual part of the Java language or the software typically downloaded to create programs (such as the JRE [Java Runtime Environment], or JDK, or GlassFish, etc). The terms "core Java" and "advanced Java" have relevance only in terms of a series of books. It's the same thing as if a new author published a book and decided to call it "Intermediate Java". It's simply a distinction made by the authors / publishers for the content to be in the specific books. So, the short of it is, they're just references to titles from books.