Why does IDE make programming easier?
With an IDE, you have everything you need in one place. It will even highlight the program line where an error occurs. Without an IDE, you need separate tools to edit, compile, and run your program - and if the compiler gives you an error message, you have to take note of the line number, and go to the corresponding line yourself.
When will you define a method as static in java?
Static keyword when used with a method, specifies that this method belongs to the class and not a particular instance of the class (a.k.a object of the class)
Ex:
public class StaticTest {
public static String getAuthorName() {
return "Anand";
}
}
Here getAuthorName is the static method and it can be accessed without instantiating an object of the class StaticTest. You can access this method as:
String authorName = StaticTest.getAuthorName();
What is the difference between auto storage class and register storage class?
register behaves like auto class but auto variables are actually local variables.They are created when the functions are created but they are destroyed when the fns is exited.But register-variables are used to speed up the operations
What are advantages and disadvantages of linear search?
1)in linear search it needs more space and time complexity.
2) in linear search if the key element is the last element and the search is from first element that is a worst case, or if the key element is the first element and the search is from last element then also is the worst case.
Bytecode creates an extra level of indirection.
The advantages of this extra level of indirection are:
Some of the disadvantages:
What is Switch Case Statement?
The switch statement immediately passes control to a case label within its body, dependant upon the evaluation of an expression. This allows us to control complex conditional and branching operations.
Each case label contains a constant expression, which must match one of the possible evaluations of the switch expression. No two case labels may hold the same constant. An optional "catch-all" label, the default label, may also be included but it must follow all other case labels.
When control passes to a case label, execution continues from that point on until the end of the switch body unless a break statement is encountered before the end of the switch body. A break statement forces execution to pass to the statement immediately following the switch body. Return and goto statements can be used in place of a break statement where required. Switch statements can also be nested if required.
Although some switch statements can be implemented as a series of if..else if.. statements, switch statements are more efficient as there's only one evaluation required rather than a separate evaluation for each if statement. Moreover, case labels allow greater control over the flow of execution.
As a simple example, suppose we wish to count the number of characters in a string along with the number of times a specific letter is used including how many of them are capitals. One way of doing this is to traverse the string and increment counters using a switch statement:
char str[]="This is a test string";
int chars=0, ts=0, capts=0;
for( int i = 0; i<sizeof(str); ++i )
{
switch( str[i] )
{
case( 'T' ): ++capts;
case( 't' ): ++ts;
default: ++chars;
}
}
printf( "String: "%s"\n", str );
printf( "The string has %d character%s in total\n", chars, chars>1?"s":"" );
printf( "There %s %d letter 't'%s in total\n", ts>1?"are":"is", ts, ts>1?"s":"" );
printf( "There %s %d capital 't'%s in total\n", capts>1?"are":"is", capts, capts>1?"s":"" );
Output:
String: "This is a test string"
The string has 22 characters in total
There are 4 letter 't's in total
There is 1 capital 't' in total
If we wanted separate counts of upper and lower case letters, then we can simply change the switch statement to suit:
switch( str[i] )
{
case( 'T' ): ++capts; break;
case( 't' ): ++ts;
}
++chars;
If were to reproduce the original switch statement using if..else if.. we'd need to use the following instead:
if( str[i] 't' )
++ts;
++chars;
Although this looks fine, we are forced to evaluate str[i] twice if it is not equal to 'T'. In the switch statement, we only need to evaluate str[i] once. If..else if.. should only be used when evaluating completely different expressions. If you're evaluating the same value or need more control over the execution path, then switch is much more efficient.
Note that in complex switch statements, the order of the case labels is important if execution is allowed to flow from once case label to the next. Case labels only indicate where execution will begin. After that they are ignored (just as goto labels are ignored). You must use break, return or goto statements to control where execution ends, otherwise execution will continue through all the following cases. Sometimes that is desirable (as shown in the example above), but often it is not. In most cases, the last statement of a case will be break statement.
What are the disadvantage of array implementation of stack?
Some immediate disadvantages:
How do I write a Java program that reads in two integers firstNumber and secondNumber?
import java.io.InputStreamReader;
import java.io.BufferedReader;
public class Sum
{
static int i;
static int j;
static int sum;
public static void main(String args[])
{
try
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter first no.");
i=Integer.parseInt(br.readLine());
System.out.println("enter second no.");
j=Integer.parseInt(br.readLine());
sum=i+j;
System.out.println("sum of both no is:="+sum);
}
catch(Exception e)
{
System.out.println("Exception is:-"+e);
}
}
}
Can you override an instance method and make it final?
No. Once a method is declared final in a class, no derivative of that class can override that method.
What is the use of encapsulation in java?
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.
If you want maintainability, flexibility, and extensibility (and I guess, you do), your design must include encapsulation. How do you do that?
• Keep instance variables protected (with an access modifier, mostly private).
• Make public accessor methods, and force calling code to use those methods rather than directly accessing the instance variable.
• For the methods, use the JavaBeans naming convention of set and get.
source- SCJP book by Kathy and Bert
Why objects are not used to acces the static class members?
Static members are members of the class, not an instance of the class.
All instances of a class have access to all their static members. Derived classes have access to both protected and public statics, while public statics are fully accessible.
Think of static members as being global variables or functions, but scoped to the class rather than being globally accessible (and therefore publicly accessible).
Consider the following example. A private static member variable, count, is declared. Note that it must be initialised immediately after the class declaration, but outside of a code block. Were it declared public it could be initialised in a code block, but the class needs to maintain control over it, thus it is completely hidden from all outside influence.
The two constructors increment count while the destructor decrements count. Thus count maintains a running total of all the instances of the class (much like a reference counter). Note that all instances of the class have direct access to the static member. Note also that if other parametrised constructors are declared, each must increment countindependently.
The public static accessor method, GetCount(), returns the current value of count (returning by value, not by reference). There is no equivalent set mutator, thus countis a read-only variable -- it cannot be altered from outside of the class.
#include
class MyClass{
public:
MyClass(){ ++count; }
MyClass(const MyClass & copy){ ++count; }
~MyClass(){ --count; }
public:
static unsigned int GetCount(){ return( count ); }
private:
static unsigned int count;
};
// Initialise the static member:
unsigned int MyClass::count = 0;
void PrintCount()
{
printf( "There %s currently %u instance%s of MyClass.\n",
MyClass::GetCount() 1 ? "" : "s" );
}
int main()
{
MyClass * myClass[10];
// At this point there are no instances.
PrintCount();
printf( "\nCreating instances of MyClass...\n" );
for( int i=0; i<10; ++i )
{
myClass[i] = new MyClass();
PrintCount();
}
printf( "\nDestroying instances of MyClass...\n" );
for( int i=0; i<10; ++i )
{
delete( myClass[i] );
PrintCount();
}
return( 0 );
}
Program for print reverse string of given number in java?
public class StringReverseExample
{
public static void main(String[] args)
{
int num=1001;
int n=num, rev;
while(num!=0)
{
int d=num%10;
rev= (rev*10)+d;
num=num/10;
}
System.uot.println(rev);
}
}
Who invented object oriented programming?
Credit for this is usually given to Alan Kay, though he was one of a number of team members (Dan Ingalls, Adele Goldberg, Ted Kaehler, Scott Wallace) at the Xerox Palo Alto Research Center. It was these people who put down the original specifications of object-oriented programming and developed the Smalltalk programming language as an implementation of these specifications.
How do you compile and run C programs on the Macintosh?
The first step is to install the XCode developer tools that comes with OSX. You can do this by looking on your OSX install disc. There should be a folder for optional installs, and a package for XCode. This will automatically install gcc, a C compiler, on your machine. Next, find the Console in the Utilities folder. It opens a UNIX shell. Move to the directory that your c file is in (you can find more information about UNIX elsewhere if you need to). Let's say your C file is named myfile.c. To compile it, type: gcc myfile.c This will compile your program, and name the compiled program a.out. To run it, type: ./a.out I hope that helps. Brina
Write a Java program that computes the summation of 1 to n?
Increment your counter two at a time. Example, for n = 100:
n = 100;
sum = 0;
for (int i = 1; i <= n; i+=2) sum += i;
Another option (less efficient) is to loop through ALL integers, and check whether the number is odd:
n = 100;
sum = 0;
for (int i = 1; i <= n; i++) {
if (i % 2 == 1)
sum += i;
}
Or:
if (min%2==0) min= min+1;
if (max%2==0) max= max-1;
sum = ((max-min)/2 + 1) * (min+max) / 2;
What does double in JAVA mean?
A double type in Java is a 64-bit IEEE 754 floating point value. The basics of this data format consists of 3 parts: base, bits, and exponent. The 64-bit specification says that the base is 2, the bits are 53, and the max exponent is 1023. Without going into too much detail, this means that the largest value that a double can store is: (2-2-52)*101023 and with a precision (smallest non-zero number) of 4.9*10-324.
What are the inbuilt final methods in java?
final methods and variables are that whose values couldn't change.not even run time also.
Java source code of odd numbers between 1 and 50?
public class apples {
public static void main(String argc[]) {
for(int i = 0; i <= 50; i++){
if(i % 2 != 0){
System.out.println(i);
}
}
}
}
What are the characteristic of Object Oriented programming?
the features of oop are:
Functions that operate on the data of an object are tied together in the data structure.
Data is Hidden and cannot be accessed by external functions.
others are :
· Robust
· Multi threaded
· Architecture Neutral
· High Performance
· Distributed
· Dynamic
If you do that, many definitions and later changes have to be done but in a single place, where they will affect the two (or more) subclasses.
What is crash recovery in dbms?
STACK ADT
OBJECTS: a finite ordered list with zero or more elements.
METHODS: for all stack Î Stack, item Î element, max_stack_size Î positive integer
Stack create S(max_stack_size) ::=create an empty stack whose maximum size is max_stack_size
Boolean isFull(stack, max_stack_size)::=
if (number of elements in stack CreateQ(max_queue_size))
return TRUE
else
return FALSE
Element dequeue(queue) ::=
if (IsEmptyQ(queue))
return
else
remove and return the item at front of queue.
What does it mean to say that Java is portable?
Java is portable because it does not compile machine level code, like most compliers, but instead makes bit level code. Because of this, to run a Java program all you need is an interpreter on your platform (be it a web browser, operating system, or cell phone) to run the code.
How do you create a two dimentional array?
A one-dimensional array is an array where each element in the array points to a specific value of the type specified by the array (all values must be of the same type). For example, we can store integer values in an integer array, character values in a character array and strings in a string array.
Multi-dimensional arrays are implemented as one-dimensional arrays where every element is itself a one-dimensional array, for as many dimensions as required. The overall size of any array (in elements) is the product of all its dimensions, thus a two-dimensional array of 4x5 elements has 20 elements in total, divided into 4 arrays of 5 elements each. However, because all the elements are allocate contiguously, any multi-dimensional array can be treated as if it were one-dimensional. Note that every element of an array must be exactly the same length, even when that element is another array.
The most common type of array we use is a pointer array (an array of pointer elements). Given that a non-null pointer does not store any size information (the number of elements being referred to), we typically use null-terminated pointer arrays, where a null pointer denotes the end of the array being referred to. This makes it possible to implement "jagged" or "irregular" multi-dimensional arrays, where each dimension can be a different length. An array of variable-length strings is an example of a jagged array, such that each element points to a null-terminated character array.
Data structure is important since it dictates the types of operations we can perform on the data and how efficiently they can be carried out. It also dictates how dynamic we can be in dealing with our data; for example it dictates if we can add additional data on the fly or if we need to know about all of the data up front. We determine which data structures to use to store our data only after we've carefully analyzed the problem and know at least what it is we hope to do with the data; for example if we'll require random access, or sequential access, or the ability to move both forward and backward through the data.
Is java a popular programming language?
No, the Java is not machine language. Java is a high-level programming language that compiles to byte code suitable for interpretation into machine code by the Java virtual machine. In other words, you program the virtual machine rather than the physical machine. Once compiled, the byte code can be executed upon any machine with a Java virtual machine implementation.