Java认证:Java笔试题附答案

来源:java认证发布时间:2012-11-12 13:05:47java认证视频


  6. 如何打印出当前源文件的文件名以及源文件的当前行号?
  答案:
  cout << __FILE__ ;
  cout<<__LINE__ ;
  __FILE__和__LINE__是系统预定义宏,这种宏并不是在某个文件中定义的,而是由编译器定义的。
  7. main主函数执行完毕后,是否可能会再执行一段代码,给出说明?
  答案:可以,可以用_onexit 注册一个函数,它会在main 之后执行intfn1(void), fn2(void), fn3(void), fn4 (void);
  void main( void )
  {
  String str("zhanglin");
  _onexit( fn1 );
  _onexit( fn2 );
  _onexit(fn3 );
  _onexit( fn4 );
  printf( "This is executed first./n" );
  }
  int fn1()
  {
  printf( "next./n" );
  return 0;
  }
  int fn2()
  {
  printf( "executed " );
  return 0;
  }
  int fn3()
  {
  printf( "is " );
  return 0;
  }
  int fn4()
  {
  printf( "This ");
  return 0;
  }
  The _onexit function is passed the address of afunction (func) to be called when the program terminates normally. Successivecalls to _onexit create a register of functions that are executed in LIFO(last-in-first-out) order. The functions passed to _onexit cannot takeparameters.
  8. 如何判断一段程序是由C 编译程序还是由C++编译程序编译的?
  答案:
  #ifdef __cplusplus
  cout<<"c++";
  #else
  cout<<"c";
  #endif
  9.文件中有一组整数,要求排序后输出到另一个文件中
  答案:
  #i nclude
  #i nclude
  using namespace std;
  void Order(vector& data)//bubble sort
  {
  int count = data.size() ;
  int tag = false ; //设置是否需要继续冒泡的标志位
  for ( int i = 0 ; i < count ; i++)
  {
  for ( int j =0 ; j < count - i - 1 ; j++)
  {
  if ( data[j] > data[j+1])
  {
  tag = true ;
  int temp = data[j] ;
  data[j] = data[j+1] ;
  data[j+1] = temp ;
  }
  }
  if ( !tag )
  break ;
  }
  }
  void main( void )
  {
  vectordata;
  ifstreamin("c://data.txt");
  if ( !in)
  {
  cout<<"file error!";
  exit(1);
  }
  int temp;
  while (!in.eof())
  {
  in>>temp;
  data.push_back(temp);
  }
  in.close(); //关闭输入文件流
  Order(data);
  ofstream out("c://result.txt");
  if ( !out)
  {
  cout<<"fileerror!";
  exit(1);
  }
  for ( i = 0 ; i < data.size() ; i++)
  out<  10. 链表题:一个链表的结点结构
  struct Node
  {
  int data ;
  Node *next ;
  };
  typedef struct Node Node ;
  (1)已知链表的头结点head,写一个函数把这个链表逆序 ( Intel)
  Node * ReverseList(Node*head) //链表逆序
  {
  if ( head == NULL || head->next == NULL )
  returnhead;
  Node *p1 = head ;
  Node *p2 = p1->next ;
  Node *p3 =p2->next ;
  p1->next = NULL ;
  while ( p3 != NULL )
  {
  p2->next = p1 ;
  p1 = p2 ;
  p2 = p3 ;
  p3 = p3->next ;
  }
  p2->next = p1 ;
  head = p2 ;
  return head ;
  }

视频学习

我考网版权与免责声明

① 凡本网注明稿件来源为"原创"的所有文字、图片和音视频稿件,版权均属本网所有。任何媒体、网站或个人转载、链接转贴或以其他方式复制发表时必须注明"稿件来源:我考网",违者本网将依法追究责任;

② 本网部分稿件来源于网络,任何单位或个人认为我考网发布的内容可能涉嫌侵犯其合法权益,应该及时向我考网书面反馈,并提供身份证明、权属证明及详细侵权情况证明,我考网在收到上述法律文件后,将会尽快移除被控侵权内容。

最近更新

社区交流

考试问答